Tim Hanson
Member
I was searching on how to get a faster matching code and came across a thread on a different forum that to use a looping function (which was posted) was 10x faster then using Match()
I am trying to piece together a macro that does this, I get the proper matching but the way it outputs the matched rows is not what I need.
I have attached a excel file with the code and a sheet that has the output and a sheet that has the desired output.
Thank you
I am trying to piece together a macro that does this, I get the proper matching but the way it outputs the matched rows is not what I need.
I have attached a excel file with the code and a sheet that has the output and a sheet that has the desired output.
Thank you
Code:
Sub test1()
Dim arrS() As Variant
Dim arrT() As Variant
Dim myresult
Dim Destination As Range
Dim wsS As Worksheet
Dim wsT As Worksheet
Dim sLC As Long
Dim sLR As Long
Dim tLC As Long
Dim tLR As Long
Dim b As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set wsS = ThisWorkbook.Sheets("YYY")
Set wsT = ThisWorkbook.Sheets("XXX")
With wsS
sLC = .Cells(1, .Columns.Count).End(xlToLeft).Column
sLR = .Range("A" & .Rows.Count).End(xlUp).Row
arrS = .Range("A1").Resize(sLR, sLC)
ReDim myresult(1 To UBound(arrS), 1 To UBound(arrS, 2))
End With
With wsT
tLC = .Cells(1, .Columns.Count).End(xlToLeft).Column
tLR = .Range("A" & .Rows.Count).End(xlUp).Row
arrT = .Range("A2:A" & tLR).Value
End With
For i = 1 To UBound(arrS)
b = Contains(arrT, arrS(i, 1))
If b = True Then
For k = 1 To UBound(arrS, 2)
myresult(i, k) = arrS(i, k)
Next k
End If
Next i
'wsT.UsedRange.ClearContents
wsT.Range("A1").Resize(UBound(arrS, 1), UBound(arrS, 2)).Offset(0, tLC) = myresult
End Sub
Code:
Function Contains(arrT, v) As Boolean
Dim rv As Boolean, lb As Long, ub As Long, i As Long
lb = LBound(arrT)
ub = UBound(arrT)
For i = lb To ub
If arrT(i, 1) = v Then
rv = True
Exit For
End If
Next i
Contains = rv
End Function