I tried to write a VLOOKUP in my macro but for some unknown reasons it didn't work (see my post on StackOverFlow for more info.
So I decided to do a macro which achieves the same result, but it takes about 20 sec to run. Any ideas on how I can improve its performance ?
What this macro is doing :
I have 2 worksheets in my workbook. Both have a column listing VINs + other info. What this macro is doing is a Vlookup from one sheet to the other one to retrieve some information when the same VIN is found.
Sub ReplacementVlookups()
'Delete Rows with no content
Dim i As Integer
i = 0
Do
i = i + 1
Loop While Range("A" & i) <> ""
Rows(i & ":" & Worksheets("All Time SMS Dump").Range("A" & Rows.Count).End(xlUp).Row).Delete (xlShiftUp)
Worksheets("All Time SMS Dump").Columns("G:G").NumberFormat = "General"
Worksheets("All Time SMS Dump").Columns("H:H").NumberFormat = "General"
'Store all Lookup values in a 1D array
Dim VLookupType As Integer
Dim j As Integer
Dim LastRow As Integer
Dim LastRowSF As Integer
Dim VINArrayAllTime As Variant
Dim VINArraySF As Variant
Dim ValuesCopied As Variant
Dim ValuesPasted As Variant
LastRow = Worksheets("All Time SMS Dump").Range("A" & Rows.Count).End(xlUp).Row
LastRowSF = Worksheets("Salesforce Dump").Range("A" & Rows.Count).End(xlUp).Row
VINArrayAllTime = Worksheets("All Time SMS Dump").Range("A2:A" & LastRow)
VINArraySF = Worksheets("Salesforce Dump").Range("C2:C" & LastRowSF)
For VLookupType = 1 To 3 ' I have 3 columns on which I want to apply the VLookup
If VLookupType = 1 Then
ValuesCopied = Worksheets("Salesforce Dump").Range("D2:D" & LastRowSF)
ValuesPasted = Worksheets("All Time SMS Dump").Range("G2:G" & LastRow)
'Do a VLOOKUP
For i = 2 To LastRow
For j = 2 To LastRowSF - 1
If VINArrayAllTime(i - 1, 1) = VINArraySF(j - 1, 1) Then
ValuesPasted(i - 1, 1) = ValuesCopied(j - 1, 1)
Exit For
End If
Next j
Next i
Worksheets("All Time SMS Dump").Range("G2:G" & LastRow) = ValuesPasted
ElseIf VLookupType = 2 Then
ValuesCopied = Worksheets("Salesforce Dump").Range("E2:E" & LastRowSF)
ValuesPasted = Worksheets("All Time SMS Dump").Range("H2:H" & LastRow)
'Do a VLOOKUP
For i = 2 To LastRow
For j = 2 To LastRowSF - 1
If VINArrayAllTime(i - 1, 1) = VINArraySF(j - 1, 1) Then
ValuesPasted(i - 1, 1) = ValuesCopied(j - 1, 1)
Exit For
End If
Next j
Next i
Worksheets("All Time SMS Dump").Range("H2:H" & LastRow) = ValuesPasted
Else:
ValuesCopied = Worksheets("Salesforce Dump").Range("F2:F" & LastRowSF)
ValuesPasted = Worksheets("All Time SMS Dump").Range("I2:I" & LastRow)
'Do a VLOOKUP
For i = 2 To LastRow
For j = 2 To LastRowSF - 1
If VINArrayAllTime(i - 1, 1) = VINArraySF(j - 1, 1) Then
ValuesPasted(i - 1, 1) = ValuesCopied(j - 1, 1)
Exit For
End If
Next j
Next i
Worksheets("All Time SMS Dump").Range("I2:I" & LastRow) = ValuesPasted
End If
Next VLookupType
End Sub