Consulting

Results 1 to 3 of 3

Thread: Solved: Append matched row data from source wsheet back to corresponding row

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location

    Solved: Append matched row data from source wsheet back to corresponding row

    I have two workbooks (ResultMatch) and (Data Source). Want to search second workbook (Data Source) for row based on matched OrderId (found in each workbook) then with found selected row from (Data Source) paste back to first workbook (ResultMatch) importantly, append found data on same row as a matched OrderId.

    Attached is a simplified sample of the workbooks. The example shows intended result in green in the ResultMatch wbook. Would operate from a button located on the ResultMatch wbook.

    Any code help really appreciated.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Add this to the results workbook

    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim TargetSh As Worksheet
    Dim SourceLastRow As Long
    Dim sourceLastCol As Long
    Dim TargetLastCol As Long
    Dim TargetRow As Long

    Set TargetSh = Workbooks("DataSource.xls").Worksheets("Sheet1")
    With TargetSh

    TargetLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With

    With ThisWorkbook.Worksheets("Sheet1")

    SourceLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    sourceLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    TargetSh.Cells(1, "B").Resize(, sourceLastCol - 1).Copy .Cells(1, sourceLastCol + 1)
    For i = 2 To SourceLastRow

    TargetRow = 0
    On Error Resume Next
    TargetRow = Application.Match(.Cells(i, TEST_COLUMN).Value, TargetSh.Columns(1), 0)
    On Error GoTo 0
    If TargetRow > 0 Then

    TargetSh.Cells(TargetRow, "B").Resize(, sourceLastCol - 1).Copy .Cells(i, sourceLastCol + 1)
    End If
    Next i
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    38
    Location
    Thanks xld - works a treat, should get me out of a hole.

    As always - really appreciate your help.

Posting Permissions

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