What is the simplest way in VBA to determine the size (i.e., number of keys) in a dictionary object?
A Dictionary object is the equivalent of a PERL associative array. Items, which can be any form of data, are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.
The . exists() function checks if a key exists. You are searching for an item so you will need to use a loop and check each item in the dictionary.
The scripting dictionary is a way to store unique items via a key and item (Keys and Items are terms in the dictionary. It is a fantastic tool to store data based on a unique key. It is powerful in that it the keys can be used to store and consolidate data.
You can assign a Key to an Item when you add the Item to the Collection, but you cannot retrieve the Key associated with an Item nor can you determine (directly) whether a key exists in a Collection. Dictionaries are much friendly and open with their keys. Dictionaries are also considerably faster than Collections.
As in a Scripting.Dictionary
? just use its .Count
property.
For the number of keys (not items) you should use .Keys
property, via UBound(Dictionary.Keys)
since its an array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With