Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| 'Arrays, Collections, Lists - How to replace an item in a collection
'Collections don't allow you to modify the value of an item. If you want to
'change the value of an item, you must first delete it and then add a new item.
'Here's generic routine that uses this technique:
' INDEX can be either a numeric or a string value.
Sub ReplaceItem(col As Collection, index As Variant, newValue As Variant)
' First remove that item (exits with error if it doesn't exist).
col.Remove index
' Then add it again.
If VarType(index) = vbString Then
' Add a new item with the same string key.
col.Add newValue, index
Else
' Add a new item in the same position (without any key).
col.Add newValue, , index
End If
End Sub |