Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a nested dictionary key exists in VBA

I am trying to use a dictionary of dictionaries in Excel VBA. What I'm trying to find out is whether the nested dictionary has a key already, and if not, add it.

My data looks like the following:

Country, Customer, Purchased
US, Alan, Lawnmower
US, Alan, Hammer
US, Karen, Donkey
US, Simon, Mustang
MX, Carl, Lawnmower
MX, Alan, Donkey
...

The data structure I have in mind looks like dictionary --> dictionary --> array -- that is, country --> customer --> purchased.

The code I am using to find out if a country does not exist in the country dictionary is:

If Not dataset.Exists(country) Then 
...

However, code that looks like the following does not work:

If Not dataset.Exists(country)(customer) Then 
.... 

How do you check the next level of dictionary entries? Is it a case of storing the contents of the country dictionary in an array, then checking that (which seems a mess)?

like image 485
Blue Otter Hat Avatar asked Feb 21 '14 13:02

Blue Otter Hat


1 Answers

You could use this one:

If Not dataset.Exists(country) Then
    'if country doesn't exists do sth
ElseIf Not dataset(country).Exists(customer) Then
    'if country exists, but customer doesn't exists do sth
End If
like image 168
Dmitry Pavliv Avatar answered Sep 30 '22 18:09

Dmitry Pavliv