Page 48 - Excel for Scientists and Engineers: Numerical Methods
P. 48
CHAPTER 2 FUNDAMENTALS OF PROGRAMMING WITH VBA 25
For...Next Loop
The syntax of the For ... Next loop is given in Figure 2-4.
For Counter = Start To End Step lncrement
statements
Next Counter
Figure 2-4. The VBA For ... Next structure.
For example,
For J = 1 To 100
statements
Next J
Figure 2-5. Example of a For ... Next loop.
The Step lncrement part of the For statement is optional. If lncrement is
omitted, it is set equal to 1. lncrement can be negative or nonintegral, for
example
For J = 100 To 0 Step -1
Do While... Loop
The Do ... Loop is used when you don't know beforehand how many times the
loop will need to be executed. You can loop While a condition is True or Until a
condition becomes True. The two possibilities are shown in Figures 2-6 and 2-7.
Do While LogicalExpression
statements
Loop
Figure 2-6. The Do While ... Loop structure.
Do
statements
Loop While LogicalExpression
Figure 2-7. Alternate form of the DO ... Loop While structure.
Note that this second form of the Do While structure executes the loop at
least once.
For Each...Next Loop
The For Each ... Next loop is a loop structure peculiar to an object-oriented