I'm using the Dictionary class in the MS Runtime Scripting library to store where labels are going to go for a report template. Is there a way to iterate over all the key value pairs in that dictionary like in Python?
I just want to use the key as the row number (It's all going in column A) and the value will be the label header.
Something like:
For Each key in dict Range("A" & key).Value = dict(key) Next key
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.
The . exists() function checks if a key exists.
The VBA dictionary object links keys to items so you can get your items later by simply calling the key by name. The VBA dictionary is a top-level object in the Microsoft Scripting Runtime object library.
Try:
For Each varKey In oDic.Keys() Range("A" & varKey).Value = oDic(varKey) Next
Note that the key iterator must be declared as a Variant
.
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