Items are added to a
Collection by calling its
.Add(item, [key], [before, after])
|item||The item to store in the |
|key||Optional. A |
|before||Optional. An existing key (|
|after||Optional. An existing key (|
Keys are not case-sensitive.
.Add "Bar", "Foo" and
.Add "Baz", "foo" will result in a key collision.
If neither of the optional before or after parameters are given, the item will be added after the last item in the
Insertions made by specifying a before or after parameter will alter the numeric indexes of existing members to match thier new position. This means that care should be taken when making insertions in loops using numeric indexes.
Public Sub Example() Dim foo As New Collection With foo .Add "One" 'No key. This item can only be retrieved by index. .Add "Two", "Second" 'Key given. Can be retrieved by key or index. .Add "Three", , 1 'Inserted at the start of the collection. .Add "Four", , , 1 'Inserted at index 2. End With Dim member As Variant For Each member In foo Debug.Print member 'Prints "Three, Four, One, Two" Next End Sub