检查VBA中是否存在嵌套字典键

Blu*_*Hat 9 excel vba dictionary data-structures

我试图在Excel VBA中使用词典字典.我想要找出的是嵌套字典是否已经有一个密钥,如果没有,添加它.

我的数据如下所示:

Country, Customer, Purchased
US, Alan, Lawnmower
US, Alan, Hammer
US, Karen, Donkey
US, Simon, Mustang
MX, Carl, Lawnmower
MX, Alan, Donkey
...
Run Code Online (Sandbox Code Playgroud)

我想到的数据结构看起来就像dictionary --> dictionary --> array- 也就是说country --> customer --> purchased.

我用来查明country字典中是否存在某个国家/地区的代码是:

If Not dataset.Exists(country) Then 
...
Run Code Online (Sandbox Code Playgroud)

但是,看起来像以下代码不起作用:

If Not dataset.Exists(country)(customer) Then 
.... 
Run Code Online (Sandbox Code Playgroud)

你如何检查下一级词典条目?这是一个将国家字典的内容存储在数组中,然后检查(这看起来一团糟)的情况?

Dmi*_*liv 5

你可以用这个:

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
Run Code Online (Sandbox Code Playgroud)