我正在尝试创建一个字典,其中包含每个键的集合。这样做的原因是我想稍后从同一个键检索多个值。在此示例中,我想要获得唯一键的总值 (val) 以及出现次数 (n):
sub update()
Dim dict As Dictionary
Dim coll As Collection
Set dict = New Dictionary
Set coll = New Collection
coll.Add 100, "val"
coll.Add 3, "n"
dict.Add "coll", coll
Debug.Print dict.item("coll")("val")
Debug.Print dict.item("coll")("n")
Run Code Online (Sandbox Code Playgroud)
到目前为止,这工作正常,当我尝试更新集合中的值时会出现问题(对象不支持此功能):
dict.item("coll")("val") = dict.item("coll")("val") + 100
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
如果我使用数组而不是集合,则不会出现错误,但值不会改变。仅当我将集合读出到变量、更改值、创建新集合、从字典中删除旧集合并添加新集合时,它才有效。
有什么办法可以像我上面的方法一样在一行中做到这一点吗?我也很高兴能找到该任务的替代解决方案。