Execute Tasks A Specific Number Of Times

You can use the For Next loop to execute a statement or a series of statements a specific number of times in your macro. For example, using a For Next loop lets you add the values in a specific number of cells.

When you use the For Next loop, the statements you specify between the For and Next statements execute until the counter variable reaches the specified maximum value. As soon as the looping structure determines that the maximum value is met, control moves to the next statement outside the loop.

The For Next loop consists of four basic parts. The For statement initiates the loop. You specify a counter variable with a initial and maximum value;such as A = 1 To 5. The inside of the body of the loop consists of a series of statements that perform until the counter meets the maximum value of the loop. Finally you mark the end of the loop with the Next statement.

When the For Next loop starts, it checks to make sure the value of the counter variable has not met the maximum value. If the variable is less than the maximum, the loop executes. The counter variable is a numeric value that is incremented by 1 each time the loop executes.

The loop continues to execute as long as the Minimum value is less than the Maximum value specified for the counter variable. If the Minimum value is initially greater than the Maximum value, the body of the loop never executes.

EXECUTE TASKS A SPECIFIC NUMBER OF TIMES

EXECUTE TASKS A SPECIFIC NUMBER OF TIMES

'—H Create a new subroutine.

^0 Declare the loop variable and any other variables needed for the subroutine.

'—0 Type For N = 1 To Max, replacing N with the variable declared for the For Next loop and Max with the maximum value of the loop.

'—H Create a new subroutine.

^0 Declare the loop variable and any other variables needed for the subroutine.

'—0 Type For N = 1 To Max, replacing N with the variable declared for the For Next loop and Max with the maximum value of the loop.

0 0