For ... Each ... Next

Top  Previous  Next

For Each...Next - Loop Through Collections

Description:  For Each...Next loops allow you to iterate through a collection of object or array elemets without having to specify a start, end or step value.

For Each...Next loops are especially useful where you can not determine the number of elements within a collection.

It's possible to prematurely exit a loop by using the Exit For statement.

Example:

Dim Items(3)

 

Items(0) = "Item 1"

Items(1) = "Item 2"

Items(2) = "Item 3"

Items(3) = "Item 4"

 

For Each Item In Items

SMEvent.Raise "Trace", Item

 

If Item = "Item 3" Then

Exit For 'Exit prematurely

End If

Next