Retrieve dictionary value using key

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,913
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My worksheet is as follows:

Code:
Key Metric1 Metric2
1     a         aa

I am trying to retrieve a value using the dictionary's key:

Code:
Dim dic As Scripting.Dictionary
    Set dic = New Scripting.Dictionary
    
    dic.Add Cells(2, 1), Cells(2, 2)
    
    Debug.Print dic.Item(1)

but it does nothing.

What is wrong?

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The Key/Value pairs can be anything you like. In your case, you're adding ranges not values. Try this instead?

Code:
Dim dic As Scripting.Dictionary

Set dic = New Scripting.Dictionary

dic.Add Cells(2, 1).Value, Cells(2, 2).Value

Debug.Print dic.Item(1)

WBD
 
Upvote 0
It should be
Code:
Debug.Print dic.Items(0)
 
Upvote 0
Thanks

Further to this, if I want to use the key to retrieve Metric1, I can use this:

Code:
dic(cells(2,1).Value)

However, how can I retrieve the value in Metric2?


(Essentially I have assigned ONE Key with TWO values. Does that break the rules for dictionaries)?
 
Last edited:
Upvote 0
The Key/Value pairs can be anything you like. In your case, you're adding ranges not values. Try this instead?

Code:
Dim dic As Scripting.Dictionary

Set dic = New Scripting.Dictionary

dic.Add Cells(2, 1).Value, Cells(2, 2).Value

Debug.Print dic.Item(1)

WBD

Thanks but this returns blank.
 
Upvote 0
You can't. Why not add the row as the value in the dictionary:

Code:
Dim dic As Scripting.Dictionary

Set dic = New Scripting.Dictionary

dic.Add Cells(2, 1).Value, 2 ' 2 is the row number

Debug.Print Cells(dic.Item(1), 2).Value
Debug.Print Cells(dic.Item(1), 3).Value

Prints "a" and "aa" on my Excel.

WBD
 
Last edited:
Upvote 0
You can't. Why not add the row as the value in the dictionary:

Code:
 Dim dic As Scripting.Dictionary

 Set dic = New Scripting.Dictionary

 dic.Add Cells(2, 1).Value, 2 ' 2 is the row number

 Debug.Print Cells(dic.Item(1), 2).Value
 Debug.Print Cells(dic.Item(1), 3).Value

Prints "a" and "aa" on my Excel.

WBD



I get a Run-time error '1004' on this line:

Code:
Debug.Print Cells(dic.Item(1), 2).Value
 
Last edited:
Upvote 0
Hmmm. Odd. Try:

Code:
Debug.Print dic.Keys(0)

That should show what's in the first key.

WBD

It shows 1.

BTW, your original suggestionnow worked, AFTER I closed down Excel and recreated it!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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