Consulting

Results 1 to 4 of 4

Thread: Update data from mapping table to another worksheet

  1. #1
    VBAX Regular
    Joined
    Apr 2016
    Posts
    67
    Location

    Update data from mapping table to another worksheet

    Hi,,

    I need help urgently on a macro to fill a spreadsheet based on a mapping table.

    Mapping sheet givves us the mapping table. Column header marked in yellow is the primary key.


    Destination will have this header in any column of the worksheet. VBA should identify the
    the column and then values under those and systematicallyy fill the rest of the columns
    again spread anywere in the destination sheet.


    For eg. Outright should have NDF, FX & Cash on the same row under respective headers.


    Attaching file for better understanding.


    Thanks
    Test.xlsm

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    1. Please rely on using the forum, and not Prive Messages

    2. If I understand, something like this

    Option Explicit
    Sub test()
        Dim wsDest As Worksheet, wsMap As Worksheet
        Dim i As Long, hcell As Range, HeadersOne As Range
        Dim colInstrumentType As Long, colProductType As Long, colProductSubType As Long, colProductCode As Long
        Set wsDest = Sheets("Destination")
        Set wsMap = Sheets("Mapping")
        
        Application.ScreenUpdating = False
        
        With Application.WorksheetFunction
            colInstrumentType = .Match("Instrument Type", wsDest.Rows(1), 0)
            colProductType = .Match("Product Type", wsDest.Rows(1), 0)
            colProductSubType = .Match("Product sub Type", wsDest.Rows(1), 0)
            colProductCode = .Match("Product Code", wsDest.Rows(1), 0)
        End With
        
        
        With wsDest
            For i = 2 To wsMap.Cells(1, 1).CurrentRegion.Rows.Count
                .Cells(i, colProductCode).Value = wsMap.Cells(i, 1).Value
                .Cells(i, colInstrumentType).Value = wsMap.Cells(i, 2).Value
                .Cells(i, colProductType).Value = wsMap.Cells(i, 3).Value
                .Cells(i, colProductSubType).Value = wsMap.Cells(i, 4).Value
            Next i
        End With
        
        Application.ScreenUpdating = True
    End Sub
    3. I always like to use meaningful names for variables, like wsDest instead of Sh1 since it helps me to follow my own code after 6 months.

    4. If you need something else, just ask
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Apr 2016
    Posts
    67
    Location
    Thanks for the response.

    However, not sure if the macro addresses the issue fully. Columns can be anywhere in the destination worksheet and there can be any number of rows for the primary key.

    So, macro has to first identify the primary key column and read its value. Based on that it has to fill in other columns as well.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    OK, your test data only used a primary key one time in Mapping sheet so it was easy to map to Destimation sheet

    Maybe you could post a more realistic example with the amounts and types of data that are expected
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •