Looping

To loop through the items in a collection you can use a For - Next loop or a For Each - Next loop.
The For Each - Next loop will iterate through the entire collection.


For - Next

You can iterate through the items of a collection using a For - Next loop
This type of loop is called an Indexed loop.
Notice that 'sMyItem' has a String data type.


For Each - Next

You can iterate through all the items of a collection using a For Each - Next loop
Using a For Each - Next loop to iterate through a collection is faster than using a For - Next loop.
The item variable must be of type Variant.
It is possible to use an Object data type but this is not recommended (see below).


MyItem As Variant

The item variable must be of type Variant when the collection contains a Value data type
For example: Boolean, Currency, Date, Double, Integer, Long, Single, String.


MyItem As Object

The item variable can be of type Object or Variant when the collection contains a Class or Object.
Try to always declare your item variable with the Variant data type when you are looping through a custom collection.

To run this code, create an empty class module called "class_Testing".


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