Refer to an item in a collection using its unique key : Collection « Data Type « VBA / Excel / Access / Word






Refer to an item in a collection using its unique key

 
Sub AccessCollection()
    Dim colNames As Collection

    Set colNames = New Collection

    colNames.Add "A", "A"
    colNames.Add "D", "Da"
    colNames.Add "Al", "Al"
    colNames.Add "Br", "Br"
    colNames.Add "S", "So"
    colNames.Add "Su", "Su"

    Debug.Print colNames(1)
    Debug.Print colNames.Item(1)
    Debug.Print colNames("Alexis")
End Sub

 








Related examples in the same category

1.Create collection
2.Removing Objects from a Collection
3.Creating and Working with Custom Collections
4.Accessing an Item in a Custom Collection
5.Item method is the default method of the Collection object
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