用Excel中的VBA重命名集合中的项目键

时间:2012-05-23 14:29:07

标签: excel vba collections

我有一个如下集合:

Set NodeColl = New Collection
NodeColl.Add "Node 1", "Node 1"
NodeColl.Add "Node 2", "Node 2"
NodeColl.Add "Node 3", "Node 3"

我想知道是否有一种简单的方法来重命名选择项的键而不影响其他项或集合本身。例如,类似于NodeColl.Items(“Node 1”)。Key =“Some string”

1 个答案:

答案 0 :(得分:3)

不,你需要添加&手动删除;

with NodeColl
    .add .Item("Node 2"), "New Key", , "Node 2"
    .remove "Node 2"
end with

或使用Dictionary允许此操作,方法是添加对 Microsoft Scripting Runtime 的引用或

dim NodeColl as object: Set NodeColl = createobject("Scripting.dictionary")
NodeColl.add "key for Node 1", "Node 1"
NodeColl.add "key for Node 2", "Node 2"
NodeColl.add "key for Node 3", "Node 3"

'//direct rename allowed;
NodeColl.Key("key for Node 2") = "New Key here"