I have below vba vlookupcode that supposed to return values in Column A, B and C. When i run the code it doesn't give me an error message however only returns the lookup value for Column C. Can someone help me understand why it doesnt return lookup values for Column A and B? Thanks
Code:
Option Explicit
Sub UpdateValues()
OptimizeVBA True
Dim startTime As Single, endTime As Single
startTime = Timer
Dim fWs As Worksheet, sWs As Worksheet
Dim slRow As Long, flRow As Long
Dim pSKU As Range, pSKU2 As Range
Dim pSKU3 As Range, luVal As Range
Dim lupSKU As Range, lupSKU2 As Range
Dim lupSKU3 As Range, outputCol As Range
Dim vlookupCol As Object
Dim i As Long
Set sWs = ThisWorkbook.Sheets("Source Data")
Set fWs = ThisWorkbook.Sheets("Source Data")
slRow = sWs.Cells(Rows.Count, 4).End(xlUp).Row
flRow = fWs.Cells(Rows.Count, 4).End(xlUp).Row
Set pSKU = sWs.Range("P2:P" & slRow)
Set lupSKU = fWs.Range("A2:A" & flRow)
Set pSKU2 = sWs.Range("R2:R" & slRow)
Set lupSKU2 = fWs.Range("B2:B" & flRow)
Set pSKU3 = sWs.Range("U2:U" & slRow)
Set lupSKU3 = fWs.Range("C2:C" & flRow)
i = 1
Set outputCol = fWs.Range(fWs.Cells(2, i), fWs.Cells(flRow, i))
Set luVal = sWs.Range("Q2:Q" & slRow)
i = 2
Set outputCol = fWs.Range(fWs.Cells(2, i), fWs.Cells(flRow, i))
Set luVal = sWs.Range("S2:S" & slRow)
i = 3
Set outputCol = fWs.Range(fWs.Cells(2, i), fWs.Cells(flRow, i))
Set luVal = sWs.Range("V2:V" & slRow)
'End Select
'Build Collection
Set vlookupCol = BuildLookupCollection(pSKU, luVal)
'Lookup the values
VLookupValues lupSKU, outputCol, vlookupCol
'Next i
endTime = Timer
Debug.Print (endTime - startTime) & " seconds have passed [VBA]"
OptimizeVBA False
Set vlookupCol = Nothing
End Sub
Function BuildLookupCollection(categories As Range, values As Range)
Dim vlookupCol As Object, i As Long
Set vlookupCol = CreateObject("Scripting.Dictionary")
For i = 1 To categories.Rows.Count
vlookupCol.Item(CStr(categories(i))) = values(i)
Next i
Set BuildLookupCollection = vlookupCol
End Function
Sub VLookupValues(lookupCategory As Range, lookupValues As Range, vlookupCol As Object)
Dim i As Long, resArr() As Variant
ReDim resArr(lookupCategory.Rows.Count, 1)
For i = 1 To lookupCategory.Rows.Count
resArr(i - 1, 0) = vlookupCol.Item(CStr(lookupCategory(i)))
Next i
lookupValues = resArr
End Sub
Sub OptimizeVBA(isOn As Boolean)
Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
Application.EnableEvents = Not (isOn)
Application.ScreenUpdating = Not (isOn)
ActiveSheet.DisplayPageBreaks = Not (isOn)
End Sub