Microsoft Office Tutorials and References
In Depth Information
Executing the Same Code Repeatedly
For counter = start To end [Step step ]
[ statements ]
[Exit For]
[ statements ]
Next [ counter ]
In this syntax,
counter is any name you want to give to the variable that keeps track
of passes through the loop.
start is a number that indicates where the loop should start counting.
end is a number that indicates when the loop should end.
step (optional) indicates how much to increment or decrement counter
with each pass through the loop. If step is omitted, counter increments
by 1 with each pass through the loop.
statements is any number of VBA statements that execute with each
pass through the loop.
Figure 3-7 shows a simple example of a For...Next loop within a sub
procedure. This loop starts at 1 and increments the Counter variable by 1 with
each pass through the loop. The loop continues until Counter reaches a
value of 10, at which point the loop is done and processing continues at
the first line after the Next statement. Within the loop, the Debug.Print
statement simply prints the current value of the Counter variable to the
Immediate window.
Figure 3-7:
A simple
For...
Next loop
in a sub
procedure.
Figure 3-7 shows the result of testing the procedure in the Immediate
window. As you can see, the Counter value displays once with each pass
through the loop. Then processing continues at the lines that use Debug.
Print to display the words “All Done.”
Search JabSto ::




Custom Search