Can I load a 2-column table into a VBA Dictionary with a single instruction?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
Suppose I have a table like this one in a sheet.
Weighted Ratings.xlsm
CD
7ProductPart#
8AK42-X3
9BM21-44
10CDL402
11DMD3-902
12EGT35
MrExcel


Can I load it into a VBA Dictionary or Collection object so that I can search on the key (Product) and get back the value (Part#)?

Or do I need to do it in a loop one row at a time?
VBA Code:
Sub DictTest()

Const rnTable As String = "Table16"
Dim NumRows As Long
NumRows = Range(rnTable).Rows.Count
Debug.Print NumRows & " rows"

Dim DictTest As New Dictionary
Dim i As Long
For i = 1 To NumRows
  DictTest.Add Range(rnTable).Cells(i, 1).Value, Range(rnTable).Cells(i, 2).Value
Next i

For i = 1 To NumRows
  Debug.Print "Part# for product " & Range(rnTable).Cells(i, 1).Value _
        & " is " & Range(rnTable).Cells(i, 2).Value
Next i

End Sub

Thanks
 
It's actually using the range object as a key, not its address.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
@shinigamilight You cannot have duplicate keys in a dictionary. Your code is loading the range address into the dictionary not the cell value.
Yea, I know that. That's why I'm not using .value suffix while entering data in dictionary. i was just trying to show you that you can enter duplicate values but it's not feasible to use it that way. If you will use .value suffix it will overwrite the previous value in the dictionary.
 
Upvote 0
No you can’t, that’s the point we are making.
 
Upvote 0
The key is the range object, as I said, not its contents. However, if you try and output it without specifying a property, you get its default member - its value. If the value were the key, you would only have two items in your code, and you would be able to access them using the value.
 
Upvote 0
The key is the range object, as I said, not its contents. However, if you try and output it without specifying a property, you get its default member - its value. If the value were the key, you would only have two items in your code, and you would be able to access them using the value.
Why are you bringing using .value to obtain a key? Everyone knows that. The duplicate values literally popup by using dic.keys()(0) using my method. that proves that keys are being stored in it. You can argue that they're "range object" but my point still stands.
 
Upvote 0
No, it doesn’t. You cannot have duplicate keys in a dictionary.

The values you are outputting as your “proof” are not the actual keys.
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

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