VBA for multiple matches with multiple criteria from different worksheet

ouvay

Board Regular
Joined
Jun 9, 2022
Messages
131
Office Version
  1. 2019
Platform
  1. Windows
I'm working on this bit of code which returns the first match as the result.

Looking for some guidance or help to rewrite this to account for multiple matches - Criteria 2 is quite important as it changes later on in the data and needs to be accounted for (I feel I had to mention this as the data snippets I've provided only show a single possibility)

Here is the code
VBA Code:
Sub Tester2()

    Dim v, shtDest, shtSrc, a1, a2, i

    Set shtDest = ThisWorkbook.Sheets("Sheet1")
    Set shtSrc = ThisWorkbook.Sheets("Sheet2")
    
    With ActiveSheet.ListObjects(1).DataBodyRange
    For i = 2 To .Rows.Count

    a1 = "'" & shtDest.Name & "'!" & shtDest.Cells(i, 7).Address(False, False)
    a2 = "'" & shtDest.Name & "'!" & shtDest.Cells(i, 9).Address(False, False)

    Debug.Print a1, a2

    v = shtSrc.Evaluate("MATCH(" & a1 & "&" & a2 & ",D2:D29&H2:H29,0)")

    If Not IsError(v) Then
        shtDest.Cells(i, 8).Value = shtSrc.Range("J2:J29").Cells(v).Value
    End If
    Next i
    End With
End Sub


Here are some image references to assist in understanding the problem.

WRONG RESULT (Result is first match over all columns)
1655088282369.png



EXPECTED RESULT (Multiple matches returned)
1655088365524.png


Here is a snippet of the data sheet (sheet2)
1655088501005.png


Would appreciate any help I could get on the matter.

TIA
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top