A Dictionary is similar to a Collection but is more useful and has
more properties. In VBA, create a new module. Then, go to the Tools
menu, choose References and check the entry for "Microsoft Scripting
Runime". Then, paste in the following code:
Dim pDict As Scripting.Dictionary
Sub AAA()
Dim N As Long
Dim KeyVal As String
' create the diction
Set pDict = New Scripting.Dictionary
' add some items
pDict.Add Key:="a", Item:=1234
pDict.Add Key:="b", Item:=2345
' list the KEYS of th dictionary
For N = 0 To UBound(pDict.Keys)
Debug.Print pDict.Keys(N)
Next N
' list keys and values
For N = 0 To UBound(pDict.Keys)
KeyVal = pDict.Keys(N)
Debug.Print "Key: " & KeyVal & " Item: " & pDict(KeyVal)
Next N
' does item with key exist?
KeyVal = "asdf"
If pDict.Exists(KeyVal) = True Then
Debug.Print "key '" & KeyVal & "' exists, with value" & vbNewLine
& _
pDict(KeyVal)
Else
Debug.Print "Key '" & KeyVal & "' does not exist."
End If
' clear out all item from the dictionary
pDict.RemoveAll
End Sub
In this code pDict is the Dictionary object. One of its properties is
Keys, which returns an array of all the key values. It also has an
Exists method to test whether a key exists in the Dictionary. There
are other useful properties and methods.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)