Using dictionary to create vlookup

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is in Sheet1:

Code:
[TABLE="width: 72"]
<tbody>[TR]
[TD]SupplierID
[/TD]
[/TR]
[TR]
[TD="align: right"]1
[/TD]
[/TR]
[TR]
[TD="align: right"]2
[/TD]
[/TR]
[TR]
[TD="align: right"]3
[/TD]
[/TR]
</tbody>[/TABLE]



This is in Sheet2:

Code:
[TABLE="width: 301"]
<tbody>[TR]
[TD]SupplierID
[/TD]
[TD]SupplierName
[/TD]
[TD]Colour
[/TD]
[TD]IQ
[/TD]
[/TR]
[TR]
[TD="align: right"]3
[/TD]
[TD]Jim
[/TD]
[TD]Red
[/TD]
[TD="align: right"]10
[/TD]
[/TR]
[TR]
[TD="align: right"]1
[/TD]
[TD]Jack
[/TD]
[TD]Blue
[/TD]
[TD="align: right"]20
[/TD]
[/TR]
[TR]
[TD="align: right"]2
[/TD]
[TD]Jill
[/TD]
[TD]Green
[/TD]
[TD="align: right"]30
[/TD]
[/TR]
</tbody>[/TABLE]

This code work perfectly to do a vlookup:

Code:
Option Explicit
Sub test()
Dim DIC As Scripting.Dictionary
Set DIC = New Scripting.Dictionary
Dim LookupArray() As Variant
LookupArray = Worksheets("Sheet2").Range("a1").CurrentRegion.Value
With DIC
        
    Dim i As Long
    
    For i = 2 To UBound(LookupArray, 1)
        
        .Item(LookupArray(i, 1)) = LookupArray(i, 2)
        
        .Item(LookupArray(i, 2)) = LookupArray(i, 3)
    
        .Item(LookupArray(i, 3)) = LookupArray(i, 4)
    
    Next i
        
    Dim SourceArray() As Variant
    
    SourceArray = Worksheets("Sheet1").Range("a1").CurrentRegion.Resize(, 4).Value
    
    For i = 2 To UBound(SourceArray, 1)
        If .Exists(SourceArray(i, 1)) Then
        
            SourceArray(i, 2) = .Item(SourceArray(i, 1))
        
            SourceArray(i, 3) = .Item(SourceArray(i, 2))
        
            SourceArray(i, 4) = .Item(SourceArray(i, 3))
        
        End If
        
    Next i
End With
Worksheets("Sheet1").Range("a1").CurrentRegion.Resize(, 4).Value = SourceArray
Set DIC = Nothing
End Sub

What I am having trouble is if Sheet2 had an extra column like this:

Code:
[TABLE="width: 377"]
<tbody>[TR]
[TD]SupplierID
[/TD]
[TD]abc
[/TD]
[TD]SupplierName
[/TD]
[TD]Colour
[/TD]
[TD]IQ
[/TD]
[/TR]
[TR]
[TD="align: right"]3
[/TD]
[TD]a
[/TD]
[TD]Jim
[/TD]
[TD]Red
[/TD]
[TD="align: right"]10
[/TD]
[/TR]
[TR]
[TD="align: right"]1
[/TD]
[TD]b
[/TD]
[TD]Jack
[/TD]
[TD]Blue
[/TD]
[TD="align: right"]20
[/TD]
[/TR]
[TR]
[TD="align: right"]2
[/TD]
[TD]c
[/TD]
[TD]Jill
[/TD]
[TD]Green
[/TD]
[TD="align: right"]30
[/TD]
[/TR]
</tbody>[/TABLE]

I change the code to this:

Code:
Sub test2()
Dim DIC As Scripting.Dictionary
Set DIC = New Scripting.Dictionary
Dim LookupArray() As Variant
LookupArray = Worksheets("Sheet2").Range("a1").CurrentRegion.Value
With DIC
        
    Dim i As Long
    
    For i = 2 To UBound(LookupArray, 1)
        
        .Item(LookupArray(i, 1)) = LookupArray(i, 3)
        
        .Item(LookupArray(i, 2)) = LookupArray(i, 4)
    
        .Item(LookupArray(i, 3)) = LookupArray(i, 5)
    
    Next i
        
    Dim SourceArray() As Variant
    
    SourceArray = Worksheets("Sheet1").Range("a1").CurrentRegion.Resize(, 5).Value
    
    For i = 2 To UBound(SourceArray, 1)
        If .Exists(SourceArray(i, 1)) Then
        
            SourceArray(i, 3) = .Item(SourceArray(i, 1))
        
            SourceArray(i, 4) = .Item(SourceArray(i, 2))
        
            SourceArray(i, 5) = .Item(SourceArray(i, 3))
        
        End If
        
    Next i
End With
Worksheets("Sheet1").Range("a1").CurrentRegion.Resize(, 5).Value = SourceArray
Set DIC = Nothing
End Sub

but it doesn't return the colour column.

What is wrong?

Thanks
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Could you please explain what you want to do?
 
Upvote 0
I am trying to use dictionaries to perform a vlookup.
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,880
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top