在 VBA 中设置集合的 Item 属性

发布于 2024-12-06 15:36:46 字数 575 浏览 0 评论 0 原文

我对这件事有多难感到惊讶,但我想这是一个快速修复,所以我会在这里询问(搜索谷歌和文档,但都没有帮助)。我有一些代码可以使用键将项目添加到集合中。当我遇到集合中已经存在的键时,我只想通过在当前值上添加一个数字来设置它。

代码如下:

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

第一次将键/值对添加到集合中时,我添加一个整数作为值。当我再次遇到该键时,我尝试向该值添加另一个整数,但这不起作用。我认为问题不在于任何类型的对象不匹配或类似的情况。我当前收到的错误消息是

运行时错误 424:需要对象

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-12-13 15:36:46

一旦将值添加到集合中,您就无法对其进行编辑。所以这是不可能的:

aColl.Item(aKey) = aColl.Item(aKey) + someValue

相反,您可以从集合中取出对象,编辑其值,然后将其添加回来。

temp = aColl.Item(aKey)
aColl.Remove aKey
aColl.Add temp + someValue, aKey

这有点乏味,但是将这三行放在 Sub 中就可以了。

当集合用作对象的容器时(与整数、双精度等“原始”变量的容器相反),它们会更加友好。您无法更改集合中包含的对象引用,但可以操作附加到该引用的对象。

顺便说一句,我认为您误解了与 Item 相关的语法。你不能说:aColl(aKey).Item。正确的语法是 aColl.Item(aKey),或者简称为 aColl(aKey),因为 Item 是 Collection 的默认方法目的。然而,我更喜欢使用完整、明确的形式......

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.

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...

习惯成性 2024-12-13 15:36:46

字典集合更通用且更省时。如果您选择了这条路线,您可以直接在下面的字典上运行一个简单的 Exists 测试,然后更新键值

Patrick Matthews 在 词典 v 集合

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

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

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
暮倦 2024-12-13 15:36:46

我认为您需要删除现有的键值对,然后再次将键添加到集合中,但使用新值

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文