Page 197 - Excel Data Analysis
P. 197
10 537547 Ch09.qxd 3/4/03 12:13 PM Page 183
AUTOMATING ACTIONS WITH MACROS 9
You can specify a different value to increment the counter
variable. By default, the counter variable for the For Next
loop increments by one each time the loop executes. If you
want to increment or decrement the counter variable by a
different numeric value, you can use the Step statement and
specify the increment value. If you specify a positive value,
the counter variable increments by that value each time the
loop cycles. If you specify a negative value, the counter
variable decrements by that value each time the loop cycles.
The For loop starts with an initial counter variable J of 2
and a maximum value of 20. Each time the loop cycles, the
counter variable increments by 2. The TotalVal variable
increments by the value of the loop. The loop executes ten
times. When the initial and maximum value of the counter
are equal, the loop executes a final time before it passes
control to the next statement outside the loop.
Example:
For J = 2 To 20 Step 2
TotalVal = TotalVal + J
Next
› Type the VBA statements ˇ Type Next to indicate the ‡ Switch to Excel and run ■ The macro executes the
for the body of the loop. end of the loop. the associated macro. contents of the For Next
loop the specified number
Note: See the section "Create a Á Type any additional code of times.
Macro Using the Visual Basic needed for your subroutine.
Editor" for more information.
183