在 VBA 中设置集合的 Item 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7562546/
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
Setting the Item property of a Collection in VBA
提问by Jean-Fran?ois Corbett
I'm surprised at how hard this has been to do but I imagine it's a quick fix so I will ask here (searched google and documentation but neither helped). I have some code that adds items to a collection using keys. When I come across a key that already exists in the collection, I simply want to set it by adding a number to the current value.
我很惊讶这有多么困难,但我想这是一个快速解决方案,所以我会在这里问(搜索谷歌和文档,但都没有帮助)。我有一些代码可以使用键将项目添加到集合中。当我遇到集合中已经存在的键时,我只想通过向当前值添加一个数字来设置它。
Here is the code:
这是代码:
If CollectionItemExists(aKey, aColl) Then 'If key already has a value
'add value to existing item
aColl(aKey).Item = aColl(aKey) + someValue
Else
'add a new item to the collection (aka a new key/value pair)
mwTable_ISO_DA.Add someValue, aKey
End If
The first time I add the key/value pair into the collection, I am adding an integer as the value. When I come across the key again, I try to add another integer to the value, but this doesn't work. I don't think the problem lies in any kind of object mis-match or something similar. The error message I currently get is
我第一次将键/值对添加到集合中时,我添加了一个整数作为值。当我再次遇到该键时,我尝试向该值添加另一个整数,但这不起作用。我认为问题不在于任何类型的对象不匹配或类似的东西。我目前得到的错误信息是
Runtime Error 424: Object Required
运行时错误 424:需要对象
回答by Jean-Fran?ois Corbett
You can't edit values once they've been added to a collection. So this is not possible:
一旦将值添加到集合中,您便无法对其进行编辑。所以这是不可能的:
aColl.Item(aKey) = aColl.Item(aKey) + someValue
Instead, you can take the object out of the collection, edit its value, and add it back.
相反,您可以从集合中取出对象,编辑其值,然后将其添加回来。
temp = aColl.Item(aKey)
aColl.Remove aKey
aColl.Add temp + someValue, aKey
This is a bit tedious, but place these three lines in a Sub and you're all set.
这有点乏味,但是将这三行放在一个 Sub 中,就可以了。
Collections are more friendly when they are used as containers for objects (as opposed to containers for "primitive" variables like integer, double, etc.). You can't change the object reference contained in the collection, but you can manipulate the object attached to that reference.
当集合用作对象的容器时(与整数、双精度等“原始”变量的容器相反),它们更友好。您不能更改集合中包含的对象引用,但可以操作附加到该引用的对象。
On a side note, I think you've misunderstood the syntax related to Item
. You can't say: aColl(aKey).Item
. The right syntax is aColl.Item(aKey)
, or, for short, aColl(aKey)
since Item
is the default method of the Collection object. However, I prefer to use the full, explicit form...
附带说明一下,我认为您误解了与Item
. 你不能说:aColl(aKey).Item
。正确的语法是aColl.Item(aKey)
,或者简而言之,aColl(aKey)
因为Item
是 Collection 对象的默认方法。但是,我更喜欢使用完整的、明确的形式......
回答by brettdj
Dictionaries
are more versatile and more time efficient than Collections
. If you went this route you could run an simple Exists
test on the Dictionary directly below, and then update the key value
Dictionaries
比 更通用,更省时Collections
。如果你走这条路,你可以Exists
直接在下面的字典上运行一个简单的测试,然后更新键值
Patrick Matthews has written an excellent article on dictionaries v collections
帕特里克马修斯写了一篇关于词典和收藏的优秀文章
Sub Test()
Dim MyDict
Set MyDict = CreateObject("scripting.dictionary")
MyDict.Add "apples", 10
If MyDict.exists("apples") Then MyDict.Item("apples") = MyDict.Item("apples") + 20
MsgBox MyDict.Item("apples")
End Sub
回答by barrowc
I think you need to remove the existing key-value pair and then add the key to the collection again but with the new value
我认为您需要删除现有的键值对,然后再次将键添加到集合中,但使用新值