Declaring

Typically collections are declared as module or global variables.
Always put the declaration on one line and the assignment on a different line.

Dim myCollection As Collection 
Set myCollection = New Collection

Using the Set keyword allows you to create the collection exactly when you need it.
It allows you to test if the collection is Nothing.

If (myCollection Is Nothing) Then 
End If

Passing Collections In



Passing Collections Out

You must use the Set keyword if you want to return a collection from a function.

Public Sub Testing() 
   Dim myCollection As Collection
   Set myCollection = MyFunction
   Debug.Print myCollection.Count
End Sub

Public Function MyFunction() As Collection
Dim myCol As Collection
   Set myCol = New Collection
   myCol.Add("mon")
   myCol.Add("tue")
   myCol.Add("wed")

   Set MyFunction = myCol
End Sub


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext