MACRO LIBRARY

Collection example

Free public access. No account or subscription required.

Files

Collection example

Code

'Demonstrates the use of collections. The Collection object has four members: Add, Remove, _
    Count, and Item. Each are demonstrated below.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    'A collection can hold any data type. I called the variable "colStrings" to simply _
        indicate that I am storing strings
    Dim colStrings As New Collection
    
    'Adding items to a collection; index starts at 1
    colStrings.Add "One fish"
    colStrings.Add "Two fish"
    colStrings.Add "Red fish"
    colStrings.Add "Blue fish"
    
    'Counting the items in a collection
    Debug.Print "Items in collection: " & colStrings.Count
    
    'Removing items from a colleciton - requires a specific index
    colStrings.Remove 1
    
    'Loop through the collection
    Dim i As Integer
    For i = 1 To colStrings.Count
        Debug.Print colStrings.Item(i)
    Next i
End Sub