vba collection add item code example

Example: vba collection add

Sub TestCollection()
    ' Creation
    Dim myCol As New Collection
    ' Add items
    With myCol
        .Add "Something"
        .Add "Smth before", , 1     ' at first position
        myCol.Add "Blue", "color"
        .Add Item:=Date, Key:="Now"
        Debug.Print myCol.Count     ' > 4
    ' Get items (you can loop through Items collection)
        Debug.Print myCol(1)        ' > Smth before
        Debug.Print myCol("color")  ' > Blue
        Debug.Print .Item("Now")    ' > 26/03/2021
    ' Delete items
        .Remove (1)
        .Remove ("color")
    ' Clear
        Set myCol = New Collection
    End With
End Sub

Tags:

Vb Example