VBA – Nested Dictionary

Nested dictionary means a dictionary within a dictionary. Some days ago last week I was trying to build one and it took me some solid 15 minutes to do it, thus I have decided that it is worth an article in my blog. So, in order to get access to the dictionary object in VBA, you need a reference to “Microsoft Scripting Runtime”:

The rest should be ok. The idea of the nested dictionary is to have a dictionary, that has key and value. And the value of this dictionary is also a dictionary. If you take a look at the picture below, the outer is our first dictionary and under outer(1) , outer(2) and outer(3) there are other dictionaries. In order to see their values, you should call them like outer(1)(10) , outer(2)(20) . The values “10” and “20” are the keys of the outer(1) and outer(2) dictionaries, correspondingly:

Ok, I guess it is way better to show it than to explain it. This is the code, creating 10 inner dictionaries in our outer dictionary:

It works and it produces what I wanted. It probably looks quite a lot of code, but about 70% is put there just to print correctly its values, so do not worry. This is the printed values in the immediate window:


Tagged with: , , ,