VBA Dictionary - for vlookup

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to use dictionary in place of vlookup. my data is Range("A1:c11"),
Expected output is in Column G. [Trying to learn and use dictionary]

=VLOOKUP(G3,$A$2:$C$11,3,0)
VBA Code:
Sub UseDictionary()
    ' Get the range of values
    Dim rg As Range
    Set rg = shData.Range("A1:C11")
    
    ' Create the dictionary
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' Fill the dictionary
    Dim cell As Range
    For Each cell In rg
        dict(cell.Value) = cell.Offset(0, 2).Value
    Next
    
    Dim row As Long
    row = 1
'     Perform the Lookups
    For Each cell In rg
        Range("H" & row).Value = dict(cell.Value)
        row = row + 1
    Next

End Sub



Book1
ABCDEFGH
1NameCountryScoresExpected Output
2SachinIndia15000NameScores
3DhoniIndia12000Sachin15000
4RahulIndia11000Dhoni12000
5KohliIndia18000Ponting16000
6SehwagIndia13000Gilchrist10000
7PontingAus16000Andre Russel4000
8GilchristAus10000Sanath Jaysurya11500
9Andre RusselWest Indies4000Jos Buttler10000
10Sanath JaysuryaSri Lanka11500
11Jos ButtlerEngland10000=VLOOKUP(G3,$A$2:$C$11,3,0)
12
Sheet1



Thanks
mg
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
try this

VBA Code:
Sub UseDictionary()
' Get the range of values
    Dim rg As Range
    Set rg = shData.Range("A1:C11")
' Create the dictionary
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
' Fill the dictionary
    Dim cell As Range
    For Each cell In rg
        dict(cell.Value) = cell.Offset(0, 2).Value
    Next
'Perform the Lookups
    Set rg = shData.Range("G3:G9")
    For Each cell In rg
        cell = dict(cell.Offset(, -1).Value)
    Next
End Sub
 
Upvote 0
Hi Yongle,

Superrrrrrb (y). Again thank you so much for your help .!!?
 
Upvote 0

Forum statistics

Threads
1,224,011
Messages
6,175,928
Members
452,684
Latest member
RRaively1

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