Class Dictionary (Scripting VBA)
Object that stores data key/item pairs. To use a Dictionary class variable it first needs to be instantiated, for example
Add
Adds a key and item pair to a Dictionary object.
An error occurs if the key already exists.
Arguments
The following arguments are required:
Key (Variant) - The key associated with the item being added.
Item (Variant) - The item associated with the key being added.
CompareMode
Sets and returns the comparison mode for comparing string keys in a Dictionary object. Possible return values are BinaryCompare - Performs a binary comparison, DatabaseCompare - Microsoft Access only. Performs a comparison based on information in your database, TextCompare - Performs a textual comparison.
An error occurs if you try to change the comparison mode of a Dictionary object that already contains data. The CompareMode property uses the same values as the compare argument for the StrComp function. Values greater than 2 can be used to refer to comparisons by using specific Locale IDs (LCID).
Count
Returns a Long (long integer) containing the number of items in a collection or Dictionary object.
The following code illustrates use of the Count property.
Dim a, d, i 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Keys 'Get the keys
For i = 0 To d.Count -1 'Iterate the array
Print a(i) 'Print key
Next
...
Exists
Returns True if a specified key exists in the Dictionary object; False if it does not.
Exists (Key)
Key: Key value being searched for in the Dictionary object.
Items
Returns an array containing all the items in a Dictionary object.
The following code illustrates use of the Items method:
Dim a, d, i 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items 'Get the items
For i = 0 To d.Count -1 'Iterate the array
Print a(i) 'Print item
Next
...
Key
Sets a key in a Dictionary string.
If key is not found when changing a key, a run-time error will occur.
Key (Key)
Key: The key value being changed.
Keys
Returns an array containing all existing keys in a Dictionary string.
The following code illustrates use of the Keys method:
Dim a, d, i 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.keys 'Get the keys
For i = 0 To d.Count -1 'Iterate the array
Print a(i) 'Print key
Next
...
Remove
Removes a member from a collection or removes a control from a Frame, Page, or form.
This method deletes any control that was added at run time. However, attempting to delete a control that was added at design time will result in an error.
Remove (Key)
Key: A member's position, or index, within a collection. Numeric as well as string values are acceptable. If the value is a number, the minimum value is zero, and the maximum value is one less than the number of members in the collection. If the value is a string, it must correspond to a valid member name.
RemoveAll
The RemoveAll method removes all key, item pairs from a Dictionary object.
The following code illustrates use of the RemoveAll method.
Dim a, d, i 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
...
a = d.RemoveAll 'Clear the dictionary