Frequent commenter snb has written a thorough page on the Scripting.Dictionary object. I resisted Dictionaries for the longest time, but lately I’ve been using them in almost every situation where I would have used a Collection object. Here’s the table of contents:
Contents
– What is a dictionary ?
– What is the purpose of a dictionary ?
– Where does the Dictionary derive from ?
– How to create a Dictionary ?
– How to populate a Dictionary ?
– Add or replace ?
– Keys
– When is a key unique ?
– Create unique keys automatically
– A list of unique elements
– Items
– The number of elements in a Dictionary
– Check the existence of a key / item in the Dictionary
– How to retrieve 1 element from the Dictionary ?
– How to use the array .Keys ?
– How to use the array .Items collection ?
– Change the key of an item
– Copy an item inside the Dictionary
– Remove an item from the Dictionary
– Adapt the contents of an item in the Dictionary
– Remove all items from the Dictionary
– Early binding and late binding
– Examples
If you use Dictionaries, you should bookmark this page. If not, you should start.
Cool, I’ll take a look. Matthewspatrick as a great post at http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html on this too.
Before I found scripting dictionaries I disliked vba as a programming language.
Check the *existence* of a key / item in the Dictionary
And yes, that`s all the value I can add to this.
@gruff999
Has been taken care of. :)
The Dictionary is just about the greatest thing since sliced bread. Except PivotTables. And Power Pivot.
And many thanks to Jeff Weir for recommending my article in the first comment :)
Patrick, that is a great article you wrote. How come your penname is matthewspatrick?
Hey, you might be interested in the next post on this blog, “Dictionaries can be rude”, because it points out a couple of pitfalls of using dictionaries. e.g. the trouble with transferring them back to the worksheet if you have more than 65536 items in them, and the fact that they are much faster if your data is sorted. And it also has a table that looks at how dictionaries stack up against some other alternatives for a very simple data set. Some of these points might make a good addition to your article.