Renaming Key of Item in Collection with VBA in Excel

collections, excel, vba

Solution

Nope, you will need to add & remove manually;

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

Or use a `Dictionary` which allows this, either by adding a reference to Microsoft Scripting Runtime or;

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"

Problem

I have a collection as follows: ``` Set NodeColl = New Collection NodeColl.Add "Node 1", "Node 1" NodeColl.Add "Node 2", "Node 2" NodeColl.Add "Node 3", "Node 3" ``` I am wondering if there is an easy way to rename the keys of selective items without affecting other items or the collection itself. For example, something like NodeColl.Items("Node 1").Key = "Some string"

Original source