检查 VBA 中是否存在嵌套字典键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21936044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a nested dictionary key exists in VBA
提问by Blue Otter Hat
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.
我正在尝试在 Excel VBA 中使用字典字典。我想知道的是嵌套字典是否已经有一个键,如果没有,添加它。
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
.
我想到的数据结构看起来像dictionary --> dictionary --> array
- 也就是说,country --> customer --> purchased
.
The code I am using to find out if a country does not exist in the country
dictionary is:
我用来确定country
字典中是否不存在一个国家的代码是:
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)?
你如何检查下一级字典条目?是否将国家字典的内容存储在数组中,然后进行检查(这看起来很混乱)?
采纳答案by Dmitry Pavliv
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