Changing nested dictionary in VBA

人走茶凉 提交于 2019-12-25 08:58:29

问题


I was about to ask a question when the solution came in one test. So I'm posting anyway and answering, so others can benefit.

The question was:

I run the code below and get a runtime error 450 - Wrong number of arguments or invalid property assignment

Dim data, tmpDict As Dictionary
Set data = New Dictionary
Set tmpDict = New Dictionary

data.Add 123, tmpDict

Set tmpDict = data.Item(123)
tmpDict.Add "somekey", 100
data.Item(123) = tmpDict

The error occur in last line. The code was simplified to focus on changing a nested dictionary in an already existing item.

How can I succeed in this?


回答1:


The solution can be achieved contracting the last 3 lines in just 1, using:

data.Item(123).Add "somekey", 100

instead of:

Set tmpDict = data.Item(123)
tmpDict.Add "somekey", 100
data.Item(123) = tmpDict


来源:https://stackoverflow.com/questions/29659656/changing-nested-dictionary-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!