The code below is the alternative to vlookup.
This lookup then copy values from Column D and E of sheet "Data" to column D and E of sheet "Master" based in the matching values in column A for both worksheets i.e. using 1 criteria only.
Can someone help on how to make the code below to lookup and match 2 criteria i.e. to lookup and match column A and B for both sheets?
Thanks in advance for help...
Option Explicit
Sub VLookup_Alternative()
Dim rng As Range, j As Range, i, lRow As Long, Dict As Object, myArray As Variant
With Sheets("Data")
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
myArray = .Range("A1").Resize(lRow, 4)
Set Dict = CreateObject("scripting.dictionary")
Dict.CompareMode = vbTextCompare
For i = 2 To UBound(myArray, 1)
Dict(myArray(i, 1)) = i
Next
End With
With Sheets("Master")
Set rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
For Each j In rng
If Dict.exists(j.Value2) Then
j.Offset(, 3) = myArray(Dict(j.Value2), 3)
j.Offset(, 4) = myArray(Dict(j.Value2), 4)
End If
Next j
End With
End Sub
This lookup then copy values from Column D and E of sheet "Data" to column D and E of sheet "Master" based in the matching values in column A for both worksheets i.e. using 1 criteria only.
Can someone help on how to make the code below to lookup and match 2 criteria i.e. to lookup and match column A and B for both sheets?
Thanks in advance for help...
Option Explicit
Sub VLookup_Alternative()
Dim rng As Range, j As Range, i, lRow As Long, Dict As Object, myArray As Variant
With Sheets("Data")
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
myArray = .Range("A1").Resize(lRow, 4)
Set Dict = CreateObject("scripting.dictionary")
Dict.CompareMode = vbTextCompare
For i = 2 To UBound(myArray, 1)
Dict(myArray(i, 1)) = i
Next
End With
With Sheets("Master")
Set rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
For Each j In rng
If Dict.exists(j.Value2) Then
j.Offset(, 3) = myArray(Dict(j.Value2), 3)
j.Offset(, 4) = myArray(Dict(j.Value2), 4)
End If
Next j
End With
End Sub