985 Objects and Add-Ins
Notice the dot (.) operator before the properties in the With
statement.
40.5 Collections
A Collection is an ordered set of items that can be referred to as a unit.
The Collection object provides a convenient way to refer to a related
group of items as a single object. The items, or members, in a Collection
need only be related by the fact that they exist in the Collection. Members
of a Collection don’t have to share the same data type.
A collection can be created the same way other objects are
created. Members can be added using the Add method and removed
using the Remove method. Specifi c members can be referred to using an
integer index. The number of members currently in a Collection is avail-
able via the Count method. Our use of Collections will be restricted to
using the (quite numerous) arsenal of Collections that are part of the
Excel Object Model, like the Rows Collection mentioned in section
40.3.1.
40.5.1 The For Each Statement in Use with Arrays and Collections
The For Each statement is a variation of the For loop unique to VBA.
This statement comes in two distinct fl avors. The fi rst variation uses the
statement to loop over an array as demonstrated in the following
function:
Function ForEachSum(Rng As Range) As Double
Dim Element As Variant
Dim Sum As Double
Sum = 0
For Each Element In Rng.Value
Sum = Sum + Element
Next Element
ForEachSum = Sum
End Function