Microsoft Office Tutorials and References
In Depth Information
If you want to exit from a For...Next loop early — that is, before the index variable reaches its
final value — use the Exit For statement.
VBA permits you to nest For...Next loops within each other. Each inner loop must be totally
within the outer loop.
For Each...Next
You use the For Each...Next loop to execute a group of statements once for each member of a
collection. It is used to go through a collection and do something to or with each member. The
syntax is
For Each item in collection
Next item
n item is the variable used to iterate through the collection and must be declared as a data
type appropriate for the members of the collection. This is usually the same data type as
the collection contains, although you can use type Object or Variant as well.
n collection is the name of the collection. The statements are executed once for each
element in the collection.
Here’s an example that sets the Color property of all objects in the collection to Blue :
Dim o As Object
For Each o In SomeCollection
o.Color = Blue
Next o
You’ll see plenty of examples of using For Each...Next when you start looking at the Outlook
Object Model.
The Do...Loop statement executes a block of statements repeatedly as long as a specified logical
condition is met. Depending on how the Do...Loop statement is written and where the logical
condition is placed, the statements may be executed no times, a single time, or multiple times.
This statement has several slightly different syntaxes. To execute statements repeatedly as long as a
logical condition is True, use the While keyword:
Do While condition
Search JabSto ::

Custom Search