Create collection : Collection « Data Type « VBA / Excel / Access / Word






Create collection

 
Sub NewEmployees()
    Dim colEmployees As New Collection
    Dim emp As Variant
    With colEmployees
        .Add Item:="John Collins", Key:="128634456"
        .Add Item:="Mary Poppins", Key:="223998765"
        .Add Item:="Karen Loza", Key:="120228876", Before:=2
    End With

    For Each emp In colEmployees
        Debug.Print emp
    Next

    MsgBox "There are " & colEmployees.Count & " employees."
End Sub

 








Related examples in the same category

1.Removing Objects from a Collection
2.Creating and Working with Custom Collections
3.Accessing an Item in a Custom Collection
4.Item method is the default method of the Collection object
5.Refer to an item in a collection using its unique key
6.Removing Items from a Custom Collection
7.Remove element with the key
8.Remove all the elements of a collection in two ways:
9.Iterating Through the Elements of a Custom Collection: use the For...Each loop to iterate through the items in a collection
10.Adding Items to a Custom Collection
11.Looping Through the Elements of a Custom Collection
12.Referencing Items in a Custom Collection
13.Using a Collection to Manipulate Multiple Instances of the FileInformation Class