Microsoft Office Tutorials and References
In Depth Information
intDays = 28
This example also shows how you can mix If…Then…Else statements inside a Select Case
There will be numerous times when you’ll need to perform a certain task several times, so
VBA provides several methods of creating loops. Loops can be divided into two categories:
iteration loops and logical loops.
Iteration loops, which are used to ensure that a specific number of repetitions have occurred,
have a definitive starting point and ending point. There are two iteration loops, both similar
in style and syntax.
The first type of iteration loop, the For…Next loop, is normally used for counting and is par
ticularly useful with arrays. The syntax of a For…Next loop is shown here.
For counter = start To end [Step step ]
statements ] [
statements ] [
counter Required numeric variable that is used as a counter. It can’t be a Boolean or
member of an array.
Required value used as the starting point of the array.
Required value used as the ending point of the array.
step Optional value by which the counter should be incremented during each itera
tion of the loop. Step value can be a positive or negative value. Default value is 1.
statements One or more optional statement lines that are executed during each iter
ation of the loop.
Exit For Optional statement used to exit the loop prematurely. Code execution
resumes at the first line following the Next counter statement.
Required statement marking the end of the For…Next loop.
You can omit the counter variable used within the Next statement, but it isn’t recommended.
By including the counter, you add an extra level of protection against programming errors.