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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create dictionaries in Visual Basic

Status
Not open for further replies.

Daedelus

Technical User
Aug 14, 2002
70
US
I am trying to create a dictionary object in a Visual Basic Macro running in Excel. The VB Help shows this example for creating and populating the object:

Dim d 'Create a variable
Set d = CreateObject(Scripting.Dictionary)
d.Add "a", "Athens" 'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Whenever I try this (I have even pasted the above example directly into my subroutine), I get a "runtime error '424': Object required" error on the "Set d = ..." line.

What am I doing wrong? I have not been able to find anything in the help screens that will tell me.
 
Need to set a reference to the extensibility in the VBE

Tools>references

and look for scripting / dictionary objects. Sorry - can't tell you which as it's not an area I have experience in

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Thanks. Actually, I just discovered a solution. I came across one help page example where in they created the dictionary with the line:

Set d = CreateObject("Scripting.Dictionary")

Apparently the argument has to be a string, even though they don't show quotes in most of their examples. Once I put the quotes in, it is running okay. (I think... I haven't got the script far enough along to actually test that what I want is happening. But at least it isn't complaining to me anymore.)

As an old hack script writer, object-oriented programming still leaves my head spinning.
 
Actually it is a typo in Help. If you check "See also" you can find other Help topics using Scripting Dictionary, (Key, for example), shows the following:

Set d = CreateObject("Scripting.Dictionary")

The " " are required.



Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top