Referencing nested dictionaries using Items instead of key names

Geordy

New Member
Joined
Nov 18, 2012
Messages
1
I was wondering if anyone could help me out. I have a large dataset that I need to push into a dictionary, into that dictionary I also have to create other dictionaries and this can go on until I fill the entire dataset into the dictionary. The problem is that I do not know beforehand what the key names will be so I need to find a way to get the key names.

I have the following test code:

Code:
Dim player As New Dictionary
player.Add "val1", "hello123"
player.Add "val2", "piet123"
player.Add "dict1", New Dictionary
player("dict1").Add "val3", "bbb"
player("dict1").Add "dict2", New Dictionary
player("dict1")("dict2").Add "val4", "222"
player("dict1")("dict2").Add "val5", "444"

Now in the intermediate I can do the following

Code:
Debug.print player items(1)
Piet123
debug.Print player("dict1")(“val3”)
bbb
But I cannot do
Code:
debug.Print player("dict1").Items(0)
or
debug.Print player("dict1")(1)

Anyone have any idea on how to get reference the key in a nested dictionary not using the keyname?

Cheers
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Unlike a collection, you can't retrieve an intem by position, only by key.
 
Upvote 0
Not sure if this helps, but you can try something like...

Code:
Sub aTest()
    Dim player As Object, dict1 As Object, v As Variant
    
    Set player = CreateObject("Scripting.Dictionary")
    Set dict1 = CreateObject("Scripting.Dictionary")
    
    player.Add "val1", "hello123"
    player.Add "val2", "piet123"
    player.Add "dict1", dict1
    
    player.Item("dict1").Add "val3", "Hello456"
    player.Item("dict1").Add "val4", "Hello789"
    
    With player("dict1")
        For Each v In .keys
            Debug.Print v & " = " & .Item(v)
        Next v
    End With
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,958
Members
449,200
Latest member
indiansth

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