I am working with two different workbooks. WB1=Destination data, WB2=Source. I am trying to do a lookup and match between the wb, using wb2 to find the match in WB1. Once the match is found, copy the row (not including value in column A, the search term). This is the range of B20:Y20. The search term and matching is in Column A in both wb. Each workbook will have a different date/number with the name. This is what I have so far and can't seem to get it to copy and paste the data correctly.

Sub FindAndCopyAdjacent()

    Dim wbDest As Workbook
    Dim wbSource As Workbook
    Dim wb As Workbook  ', wb As Workbook
    
    ' Loop through all open workbooks to find the destination workbook
    For Each wb In Workbooks
        If InStr(1, wb.Name, "Table Games Lines", vbTextCompare) > 0 Then
            Set wbDest = wb
            Exit For
        End If
    Next wb
    
    ' Loop through all open workbooks to find the source workbook
    For Each wb In Workbooks
        If InStr(1, wb.Name, "IGTHeadcount", vbTextCompare) > 0 Then
            Set wbSource = wb
            Exit For
        End If
    Next wb
    
    
    Dim wsSource As Worksheet, wsLookup As Worksheet, wsTarget As Worksheet
    Dim searchValue As Variant
    Dim rngFound As Range, firstAddress As String
    Dim lastRow As Long, targetRow As Long
    
    ' Set your sheets
    Set wsSource = wb.Sheets("Headcount") ' Sheet with values to search
    Set wsLookup = wb.Sheets("Headcount") ' Sheet where matches are found
    Set wsTarget = wbDest.Sheets("Previous Wk (MONDAY)") ' Sheet to paste results
    
    ' Example: search value from Sheet1 cell A2
    searchValue = wsSource.Range("A").Value
    
    ' Validate search value
    If IsEmpty(searchValue) Then
        MsgBox "Search value is empty. Please enter a value in Sheet1!A2.", vbExclamation
        Exit Sub
    End If
    
    ' Find first match in lookup sheet (searching Column A)
    With wsLookup.Range("A:A")
        Set rngFound = .Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
        
        If rngFound Is Nothing Then
            MsgBox "No match found for '" & searchValue & "'.", vbInformation
            Exit Sub
        End If
        
        firstAddress = rngFound.Address
        targetRow = wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1
        
        ' Loop through all matches
        Do
            ' Copy adjacent cell (Column B in this example) to target sheet
            wsTarget.Cells(targetRow, "A").Value = rngFound.Value
            wsTarget.Cells(targetRow, "B").Value = rngFound.Offset(0, 1).Value
            
            targetRow = targetRow + 1
            
            ' Find next match
            Set rngFound = .FindNext(rngFound)
        Loop While Not rngFound Is Nothing And rngFound.Address <> firstAddress
    End With
    
    MsgBox "Copy complete.", vbInformation
End Sub

Table games lines worksheets Previous Wk (MONDAY). Of course this will change is day of the week. where data is going to go:
enter image description here

IGTHeadcount worksheets Headcount where i am getting data and using it to get name to lookup to match to TGL:
enter image description here

7 Replies 7

If you are looking for partial matches then then , LookAt:=xlWhole should be , LookAt:=xlPart? In range.find

It would help your post to include some examples of search values and the values being compared in ColA.

searchValue = wsSource.Range("A").Value
should be
searchValue = wsSource.Range("A2").Value

Also - wsSource and wsLookup are the same sheet?

Set wsSource = wb.Sheets("Headcount") ' Sheet with values to search - should this be Set wsSource = wbSource.Sheets("Headcount") ' Sheet with values to search? (And the same with the next line too...)

I have added a screenshot of both workbooks hopefully to give a better idea of what I am looking at and trying to do. Each book has a Title and date or number following example Table Games Line 11-09-25.xlsx

Please add the sheet names for the screenshots you posted. Aslo A7 in the first screenshot has the search value "Blackjack", but there are two "Blackjack" lines in the second screenshot.

Your code will retrieve multiple matches (each being pasted to its own line) but if that happens then data will be overwritten on the destination sheet. As written, the question's problem description, code and screenshots don't match up.

"Blackjack" followed in the next cell by "Dbl Dck Pitch" seems like it should match "Blackjack DD" on the lookup sheet? Can you please add some explanation of exactly how matches should be made - it's not clear right now that your current approach can work.

updated workheets names

TGL-worksheets("Previous Wk (MONDAY)")

IGTHeadcount-worksheets("Headcount")

The second Pic is what I will be using to search from. I will be editing the other document to match the terms as needed. So using the second pic..search 21+3, find match in first pic, copy row B:Y in second pic when match is found and past to corresponding term in pic 1 cell D19 and across. Basicly taking the numbers from second pic pasting into 1 pic basic off the terms since they are not in order to match a direct pull.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.