gpt4 book ai didi

vba - 如何更改集合中某个项目的值

转载 作者:行者123 更新时间:2023-12-01 23:07:56 24 4
gpt4 key购买 nike

使用此代码(在 excel-vba 中),我根据数组向集合添加多个项目。
我使用数组的值作为键,使用字符串“NULL”作为添加的每个项目的值。

Dim Coll As New collection
Dim myArr()

Set Coll = New collection
myArr() = Array("String1", "String2", "String3")

For i = LBound(myArr) To UBound(myArr)
Coll.Add "NULL", myArr(i)
Next i

现在,如果我想更改某个项目的值,通过键识别它,我必须删除该项目,然后添加具有相同键的项目,或者是否可以更改项目值?

这是唯一的方法吗?

Coll.Remove "String1"
Coll.Add "myString", "String1"

或者有类似的东西:(我知道这不起作用)

Coll("String1") = "myString"

最佳答案

您还可以编写一个(公共(public))函数来更新集合。

public function updateCollectionWithStringValue(coll as Collection, key as string, value as string) as collection
coll.remove key
coll.add value, key
set updateCollectionWithStringValue = coll
end function

您可以通过以下方式调用此函数:

set coll = updateCollectionWithStringValue(coll, "String1","myString")

然后你就可以调用一个衬垫了。

关于vba - 如何更改集合中某个项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com