Для задач этого типа лучше переместить эти данные в Variant Arrays и выполнить их циклическое повторение ( намного быстрее). Кроме того, сопоставление с образцом может быть обобщено на основе данных, что обеспечивает более многократно используемое решение и разделение проблем
Функция сравнения
Private Function CompareColumns(Table1 As Range, Table2 As Range, ColPairs() As Variant, Optional IsMatch As Variant = True, Optional NoMatch As Variant = False) As Variant
Dim Table1Data As Variant
Dim Table2Data As Variant
Dim OutputData As Variant
Dim rw1 As Long, rw2 As Long
Dim Col As Long
Dim FoundMatch As Boolean
' Move data to variant arrays
Table1Data = Table1.Value2
Table2Data = Table2.Value2
' Size return array
ReDim OutputData(1 To UBound(Table1Data, 1), 1 To 1)
' Loop the arrays
For rw2 = 1 To UBound(Table2Data, 1)
OutputData(rw2, 1) = NoMatch ' initialise
For rw1 = 1 To UBound(Table1Data, 1)
FoundMatch = True
For Col = LBound(ColPairs, 1) To UBound(ColPairs)
If Table1Data(rw1, ColPairs(Col, 1)) <> Table2Data(rw2, ColPairs(Col, 2)) Then
FoundMatch = False ' column not a match, move to next row
Exit For
End If
Next
If FoundMatch Then ' found a match
OutputData(rw2, 1) = IsMatch
Exit For ' exit Table2 loop when match found
End If
Next
Next
' Return result to caller
CompareColumns = OutputData
End Function
Используйте это так
Sub Compare()
Dim ws As Worksheet
Dim Table1 As Range
Dim Table2 As Range
Dim Output As Range
Dim OutputTable As Variant
Dim ColPairs() As Variant
Set ws = ActiveSheet ' update to suit your needs
' Set up ranges by any means you choose
With ws
Set Table1 = .Range(.Cells(2, 1), .Cells(.Rows.Count, 3).End(xlUp))
Set Table2 = .Range(.Cells(2, 10), .Cells(.Rows.Count, 8).End(xlUp))
Set Output = .Cells(2, 13).Resize(Table2.Rows.Count, 1)
End With
'Specify columns to compare
ReDim ColPairs(1 To 3, 1 To 2)
ColPairs(1, 1) = 1: ColPairs(1, 2) = 3
ColPairs(2, 1) = 2: ColPairs(2, 2) = 2
ColPairs(3, 1) = 3: ColPairs(3, 2) = 1
' Call Match function
OutputTable = CompareColumns(Table1, Table2, ColPairs, "Yes", "No")
' Place Output on sheet
Output = OutputTable
End Sub