Nested Dict in VBA

krisLB7

New Member
Joined
Jun 25, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I'm struggling with something simple that I feel I am missing the obvious.

I am looking to nest a dictionary that has ~25 pairs in it into a dictionary. The final structure I'm expecting is:


dict1 = { "parent", dict2 }

dict2 = { "a", 1 }, { "b", 2 }, ....., { "e", 6 }
------
{
"parent" : {
"a" : 1,
"b" : 2,
"c" : 3,
"d" : 4
}
}

But it's setting a null value for the parent value instead, and the debug print is err'ing out at that part as well. I'm just looking to nest a dictionary, not create a dictionary of dictionaries.

------
Sub main()

Dim dict2 As Object
Set dict2 = CreateObject("Scripting.Dictionary")

dict2.Add "a", 1
dict2.Add "b", 2
dict2.Add "c", 3
dict2.Add "d", 4

Dim dict1 As Object
Set dict1 = CreateObject("Scripting.Dictionary")

dict1.Add "parent", dict2

Debug.Print "--dict2-------"
Dim okey2 As Variant
For Each okey2 In dict2.keys
Debug.Print okey2, dict2(okey2)
Next

Debug.Print "--dict1-------"
Dim okey1 As Variant
For Each okey1 In dict1.keys
Debug.Print okey1, dict1(okey1)
Next


End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You cannot debug.print the item of dict1 as it's another dictionary.
Also I have no idea what you mean by
it's setting a null value for the parent value instead,
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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