[VBA] Adding a dictionary as the items of another dictionary

PipBoy808

Board Regular
Joined
Oct 30, 2013
Messages
107
Hello,

I have two dictionary objects populated with various keys and items - Dict1 and Dict 2. I've read that the items of a dictionary object can be quite varied, even another dictionary! I know how to add items to keys if those items are strings say, but could anyone tell me how to add an entire dictionary (Dict2) as the items of another dictionary (Dict1)?

Thanks,

Pip
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello,

I have two dictionary objects populated with various keys and items - Dict1 and Dict 2. I've read that the items of a dictionary object can be quite varied, even another dictionary! I know how to add items to keys if those items are strings say, but could anyone tell me how to add an entire dictionary (Dict2) as the items of another dictionary (Dict1)?
See if this mini-example gives you the idea of how to to that...

Code:
Sub DictionaryTest()

  Dim Dict1 As Object, Dict2 As Object
  
  Set Dict1 = CreateObject("Scripting.Dictionary")
  Dict1.Add "FirstCity", "Athens"     'Add some keys and items
  Dict1.Add "SecondCity", "Belgrade"
  Dict1.Add "ThirdCity", "Cairo"
  
  Set Dict2 = CreateObject("Scripting.Dictionary")
  Dict2.Add "MyDict1", Dict1

  MsgBox Dict2("MyDict1").Item("FirstCity")
  MsgBox Dict2("MyDict1").Item("SecondCity")
  MsgBox Dict2("MyDict1").Item("ThirdCity")

  Set Dict1 = Nothing
  Set Dict2 = Nothing

End Sub
 
Upvote 0
See if this mini-example gives you the idea of how to to that...

Rich (BB code):
Sub DictionaryTest()

  Dim Dict1 As Object, Dict2 As Object
  
  Set Dict1 = CreateObject("Scripting.Dictionary")
  Dict1.Add "FirstCity", "Athens"     'Add some keys and items
  Dict1.Add "SecondCity", "Belgrade"
  Dict1.Add "ThirdCity", "Cairo"
  
  Set Dict2 = CreateObject("Scripting.Dictionary")
  Dict2.Add "MyDict1", Dict1

  MsgBox Dict2("MyDict1").Item("FirstCity")
  MsgBox Dict2("MyDict1").Item("SecondCity")
  MsgBox Dict2("MyDict1").Item("ThirdCity")

  Set Dict1 = Nothing
  Set Dict2 = Nothing

End Sub

Just as an aside, for the red highlighted lines of code,I used the Item property because I thought it makes the syntax a bit clearer, but it is not necessary to do it that way. Just as

Rich (BB code):
MsgBox Dict1("FirstCity")

is a shortcut syntax for this...

Rich (BB code):
MsgBox Dict1.Item("FirstCity")

we can use the same kind of shortcut syntax for the Dict2 elements. Assuming the double parentheses does "throw you for loop", you could also write the red highlighted lines of code like this as well...

Rich (BB code):
  MsgBox Dict2("MyDict1")("FirstCity")
  MsgBox Dict2("MyDict1")("SecondCity")
  MsgBox Dict2("MyDict1")("ThirdCity")
 
Upvote 0
See if this mini-example gives you the idea of how to to that...

Code:
Sub DictionaryTest()

  Dim Dict1 As Object, Dict2 As Object
  
  Set Dict1 = CreateObject("Scripting.Dictionary")
  Dict1.Add "FirstCity", "Athens"     'Add some keys and items
  Dict1.Add "SecondCity", "Belgrade"
  Dict1.Add "ThirdCity", "Cairo"
  
  Set Dict2 = CreateObject("Scripting.Dictionary")
  Dict2.Add "MyDict1", Dict1

  MsgBox Dict2("MyDict1").Item("FirstCity")
  MsgBox Dict2("MyDict1").Item("SecondCity")
  MsgBox Dict2("MyDict1").Item("ThirdCity")

  Set Dict1 = Nothing
  Set Dict2 = Nothing

End Sub

The best way to learn something. Thanks!
 
Upvote 0
Who one quick question. Why do you set the dictionaries as nothing at the end?

There has been a huge debate raging across the years whether it is necessary to set objects to Nothing or not in VB (both VBA and the compiled version of VB, which was the predecessor to VBA and, come to think of it, maybe even in the VB.NET world as well). Theoretically, it is not necessary, but then there are those who point to horror stories in certain circumstance if you don't. Since it can't hurt to set objects to Nothing when you are through with them, I tend to do so. You can do a Google search for...

vba set objects nothing

and you will get more than a million links you can click to read about this.
 
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,908
Members
444,694
Latest member
JacquiDaly

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