+ Reply to Thread
Results 1 to 4 of 4

Thread: Search match value and insert data in rows

  1. #1
    Registered User
    Join Date
    01-09-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Search match value and insert data in rows

    Hi,
    I've browsed related threads but I failed to find a relevant topic for my question.
    I have two master data. Some records need to be duplicated.
    Please refer to my attached file for details. My desired results is tabulated in sheet name "output".
    Your help and guidance are appreciated.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Search match value and insert data in rows

    You could do this with a macro. Add the code to a module and run the macro genOutput - see attached workbook
    Option Explicit
    Sub genOutput()
    On Error GoTo errHandler
    Dim k As Long, wsData1 As Worksheet, wsData2 As Worksheet, wsOutput As Worksheet
    Dim compData, t As Long, branchLoc As String, fnd As Boolean
    Set wsData1 = Worksheets("Data1")
    Set wsData2 = Worksheets("Data2")
    Set wsOutput = Worksheets("output")
    wsOutput.Range("A2:E" & wsOutput.Cells(Rows.Count, "A").End(xlUp).Row).Clear
    compData = wsData2.Range("A2:B" & wsData2.Cells(Rows.Count, "A").End(xlUp).Row)
    For k = 2 To wsData1.Cells(Rows.Count, "A").End(xlUp).Row
    wsData1.Range("A" & k).Resize(, 4).Copy
    For t = LBound(compData) To UBound(compData)
    If wsData1.Range("C" & k) = compData(t, 1) Then
    branchLoc = compData(t, 2): fnd = True
    wsOutput.Range("A" & wsOutput.Cells(Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial _
            Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    wsOutput.Range("E" & wsOutput.Cells(Rows.Count, "E").End(xlUp).Row + 1).Value = compData(t, 2)
    End If
    Next
    If fnd = False Then
    wsOutput.Range("A" & wsOutput.Cells(Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial _
            Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    wsOutput.Range("E" & wsOutput.Cells(Rows.Count, "E").End(xlUp).Row + 1).Value = "Not Found"
    wsOutput.Range("E" & wsOutput.Cells(Rows.Count, "E").End(xlUp).Row).Interior.Color = 255
    End If
    fnd = False
    Next
    Exit Sub
    errHandler:
    MsgBox "Error generating output - Error " & Err.Number & _
    " - " & Err.Description
    End Sub
    Attached Files Attached Files
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Search match value and insert data in rows

    as an option
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    01-09-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Search match value and insert data in rows

    Thanks smuzoen and nilem. both of your codes are working!
    It's very helpful.
    thank you very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0