I think using dictionaries will make the code faster.
Below is code that does the same task, but it uses a Dictionary object. On my computer, it is about 100 times faster than your own code (it is tested on two sheets of 5 thousand lines each, for large data sets the gain should be even better).
Public Function TestVLookupArray2() Dim dict As Object Dim result As Variant Dim i As Long Dim destination As Excel.Range 'Load values from Sheet1 into Dictionary. Set dict = getDataFromSheetAsDictionary(Sheets("Sheet1")) result = getDataFromSheet(Sheets("Sheet2")) For i = LBound(result, 1) To UBound(result, 1) With dict If .exists(result(i, 1)) Then result(i, 2) = .Item(result(i, 1)) Else result(i, 2) = "NULL" End If End With Next i With Sheets("Sheet2") Set destination = .Range(.Cells(1, 1), .Cells(UBound(result, 1), UBound(result, 2))) destination = result End With End Function Private Function getDataFromSheet(wks As Excel.Worksheet) As Variant Dim lastRow As Long With wks lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row getDataFromSheet = .Range(.Cells(1, 1), .Cells(lastRow, 2)) End With End Function Private Function getDataFromSheetAsDictionary(wks As Excel.Worksheet) As Object Dim i As Long Dim key As String Dim value As Variant Dim arr As Variant Set getDataFromSheetAsDictionary = VBA.CreateObject("Scripting.Dictionary") arr = getDataFromSheet(wks) With getDataFromSheetAsDictionary For i = LBound(arr, 1) To UBound(arr, 1) If Not .exists(arr(i, 1)) Then Call .Add(arr(i, 1), arr(i, 2)) End If Next i End With End Function
Please note that this code consists of 3 separate functions, you need to include all of them.
Here is an article introducing dictionaries: http://www.techbookreport.com/tutorials/vba_dictionary.html
If you have any questions about this code, let me know in the comments.
mielk source share