Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic naming of objects equal to variable value 1

Status
Not open for further replies.

grif999

Technical User
Jun 30, 2006
5
US
I am trying to create a scripting dictionary, preferably, with the name of the dictionary equal to the current value of a variable during processing.
For instance, say the value of variable strCity is "Memphis". I would like to create the dictionary with the name "Memphis", to list all of the attributes associated with Memphis.
Is this possible in VBA, and if so, how?
 
Perhaps a Dictionary of Dictionary objects ?
myDict.Add Key:=strCity, Item:=CreateObject("Scripting.Dictionary")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Now we're getting somewhere. Thanks.
But, how would I go about referencing the object created as the "Item"?
 
Proof of concept:
Code:
Sub test()
Dim myDict As New Scripting.Dictionary
Dim strCity, strAttr
myDict.Add "Memphis", CreateObject("Scripting.Dictionary")
myDict.Item("Memphis").Add "Attribute M1", "Value M1"
myDict.Item("Memphis").Add "Attribute M2", "Value M2"
myDict.Item("Memphis").Add "Attribute M3", "Value M3"
myDict.Add "Austin", CreateObject("Scripting.Dictionary")
myDict.Item("Austin").Add "Attribute A1", "Value A1"
myDict.Item("Austin").Add "Attribute A3", "Value A3"
For Each strCity In myDict.Keys
  Debug.Print "Dump of " & strCity
  For Each strAttr In myDict.Item(strCity).Keys
    Debug.Print strAttr & " = " & myDict.Item(strCity).Item(strAttr)
  Next
Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Perfect!!!!!!!!!!!!!
Thank you SO much!
Ron
 
grif999 - it is customary when one is pleased enough with information to type !!!!!!!!!!, to give a star to the person who posted that information. It both shows appreciation, and gives other a hint that the thread may be useful.

Not that PH needs stars...but it seems to me that this was a very helpful response for you.

Wink wink, nudge nudge.

Gerry
My paintings and sculpture
 
Thanks, Gerry.
This is my first use of this site, and I appreciate the guidance. I certainly want to behave appropriately.
Ron
 
Thanks, Gerry.
I appreciate the guidance.
This is my first use of this site, and I certainly want to behave appropriately.
Ron
 
Well welcome aboard! You will, hopefully, find quite a bit of really useful material here. There are some FAQ available, both on subject matter, and on how to use the site.

Have fun, and remember - there are NO dumb questions.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top