MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Scripting Dictionary object


Properties and Methods
A Scripting Dictionary object stores information in Key/Item pairs. The Keys must be unique and not an array but the associated Items can be repeated (their uniqueness is held by the companion Key) and can be of any type of variant or object.

A dictionary can be thought of as a two field in-memory database with a primary unique index on the first 'field' (the Key). This unique index on the Keys property allows very fast 'lookups' to retrieve a Key's associated Item value.

Sample Code


'Populate, enumerate, locate and remove entries in a dictionary that was created
'with late binding
Sub iterateDictionaryLate()
 Dim k As Variant, dict As Object

 Set dict = CreateObject("Scripting.Dictionary")
 dict.CompareMode = vbTextCompare 'non-case sensitive compare model

 'populate the dictionary
 dict.Add Key:="Red", Item:="Balloon"
 dict.Add Key:="Green", Item:="Balloon"
 dict.Add Key:="Blue", Item:="Balloon"
'iterate through the keys
 For Each k In dict.Keys
 Debug.Print k & " - " & dict.Item(k)
 Next k
 'locate the Item for Green
 Debug.Print dict.Item("Green")

 'remove key/item pairs from the dictionary
 dict.Remove "blue" 'remove individual key/item pair by key
 dict.RemoveAll 'remove all remaining key/item pairs
End Sub
'Populate, enumerate, locate and remove entries in a dictionary that was created
'with early binding (see Remarks)
Sub iterateDictionaryEarly()
 Dim d As Long, k As Variant
 Dim dict As New Scripting.Dictionary

 dict.CompareMode = vbTextCompare 'non-case sensitive compare model

 'populate the dictionary
 dict.Add Key:="Red", Item:="Balloon"
 dict.Add Key:="Green", Item:="Balloon"
 dict.Add Key:="Blue", Item:="Balloon"
 dict.Add Key:="White", Item:="Balloon"

 'iterate through the keys
 For Each k In dict.Keys
 Debug.Print k & " - " & dict.Item(k)
 Next k
 'iterate through the keys by the count
 For d = 0 To dict.Count - 1
 Debug.Print dict.Keys(d) & " - " & dict.Items(d)
 Next d

 'iterate through the keys by the boundaries of the keys collection
 For d = LBound(dict.Keys) To UBound(dict.Keys)
 Debug.Print dict.Keys(d) & " - " & dict.Items(d)
 Next d

 'locate the Item for Green
 Debug.Print dict.Item("Green")
 'locate the Item for the first key
 Debug.Print dict.Item(dict.Keys(0))
 'locate the Item for the last key
 Debug.Print dict.Item(dict.Keys(UBound(dict.Keys)))

 'remove key/item pairs from the dictionary
 dict.Remove "blue" 'remove individual key/item pair by key
 dict.Remove dict.Keys(0) 'remove first key/item by index position
 dict.Remove dict.Keys(UBound(dict.Keys)) 'remove last key/item by index position
 dict.RemoveAll 'remove all remaining key/item pairs
End Sub


Conclusion

In this page (written and validated by ) you learned about VBA Scripting Dictionary object . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Working with ADO.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.