Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over VBA Dictionaries?

Tags:

excel

vba

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 
like image 484
mandroid Avatar asked Aug 18 '09 20:08

mandroid


People also ask

What is the difference between collection and Dictionary in VBA?

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.

How do you check if a key exists in a Dictionary VBA?

The . exists() function checks if a key exists.

What is scripting Dictionary VBA?

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.


1 Answers

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.

like image 166
EBGreen Avatar answered Sep 21 '22 06:09

EBGreen