VBA Flow control structures
The For loop is used to repeat the enclosed section of code a given number of times. The following simple example illustrates the basic syntax:
The code above declares an Integer i. The For loop assigns every value between 1 and 10 to i and then executes Debug.Print i - i.e. the code prints the numbers 1 through 10 to the immediate window. Note that the loop variable is incremented by the Next statement, that is after the enclosed code executes as opposed to before it executes.
By default, the counter will be incremented by 1 each time the loop executes. However, a Step can be specified to change the amount of the increment as either a literal or the return value of a function. If the starting value, ending value, or Step value is a floating point number, it will be rounded to the nearest integer value. Step can be either a positive or negative value.
In general a For loop would be used in situations where it is known before the loop starts how many times to execute the enclosed code (otherwise a Do or While loop may be more appropriate). This is because the exit condition is fixed after the first entry into loop, as this code demonstrates:
A For loop can be exited early with the Exit For statement: