+ Reply to Thread
Results 1 to 3 of 3

Thread: Matching ID columns from two worksheets

  1. #1
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    12

    Matching ID columns from two worksheets

    I want to loop through value in two columns and find wher they match in two separate worksheets and then concatenate the result, i have attached a sample workbook to explain what im trying to do with the value entered the way they should appear.
    Attached Files Attached Files
    Last edited by davesexcel; 10-29-2010 at 08:53 AM. Reason: What good is the thread without the sample workbook?

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Matching ID columns from two worksheets

    Try this:


    Option Explicit
    
    Sub CollateValues()
    
    Dim AWF As WorksheetFunction
    Dim lMasterLR As Long   ' Last Row on Master
    Dim lLookupLR As Long   ' Last Row on Lookup
    Dim lMasterLC As Long   ' Loop Counter on Master
    Dim lLookupLC As Long   ' Loop Counter on Lookup
    Dim lLookupMR As Long   ' Matched Row on Lookup
    Dim sLookupValues As String ' Store values
    
    Set AWF = WorksheetFunction
    With Sheets("Master")
        ' establish last row to be processed on Master sheet
        lMasterLR = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    With Sheets("Lookup")
        ' establish last row to be processed on Lookup sheet
        lLookupLR = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    With Sheets("Master")
        ' For each row on the Master sheet (excluding header)
        For lMasterLC = 2 To lMasterLR
            On Error Resume Next
            ' check if ID is found
            lLookupMR = 0
            lLookupMR = AWF.Match(.Range("A" & lMasterLC), _
                        Sheets("Lookup").Range("A:A"), 0)
            On Error GoTo 0
            ' If at least one record is found, process row(s)
            If lLookupMR <> 0 Then
                sLookupValues = Sheets("Lookup").Range("B" & lLookupMR)
                For lLookupLC = lLookupMR + 1 To lLookupLR
                    If Sheets("Lookup").Range("A" & lLookupLC) = "" Then
                        sLookupValues = sLookupValues & ", " & _
                                        Sheets("Lookup").Range("B" & lLookupLC)
                    Else
                        ' stop loop if value not blank = next ID
                        Exit For
                    End If
                Next ' lLookupLC
                ' on exiting inner loop, store values on Master sheet
                .Range("B" & lMasterLC).Value = sLookupValues    ' for production
                '.Range("C" & lMasterLC).Value = sLookupValues    ' for testing
            End If
        Next ' lMasterLC
    ' exited outer loop, processing complete
    End With
    End Sub

    Regards

  3. #3
    Registered User
    Join Date
    10-15-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Matching ID columns from two worksheets

    Works perfectly, your help is very much appreciated

+ 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