Microsoft Office Tutorials and References
In Depth Information
Executing the Same Code Repeatedly
You can use the optional To keyword of the Case statement to specify a
range of values to compare against. In the following code, statements after
Case 1 To 9 execute only if SomeNumber contains a value from 1 through
9. Statements after Case 10 To 99 execute only if SomeNumber contains a
value from 100 through 999, and so on.
Select Case SomeNumber
Case 1 to 9
Statements for when SomeNumber is between 1 and 9
Case 10 to 99
Statements for when SomeNumber is between 10 and 99
Case 100 to 999
Statements for when SomeNumber is between 100 and 999
End Select
The ranges used in a Case statement include the endpoints. For example,
Case 1 to 9 includes 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Executing the Same Code Repeatedly
Occasionally, you want to execute one or more VBA statements multiple
times. Suppose that you write some VBA statements that need to operate
on each record in a table, and the table contains 1,000 records. You have
two choices: Write each set of statements 1,000 times, or create a loop that
repeats a single set of statements 1,000 times. Needless to say, typing the
statements once rather than 1,000 times saves you a lot of time. A loop is
your best bet.
Using Do...Loop to create a loop
The Do...Loop block is one method of setting up a loop in code to execute
statements repeatedly. Two syntaxes for using Do...Loop exist. The first
syntax evaluates the condition of the loop, as follows:
Book VIII
Chapter 3
Do [{While | Until} condition ]
[ statements ]
[Exit Do]
[ statements ]
Loop
The second syntax provides the option of defining the condition at the
bottom of the loop, using this syntax:
Do
[ statements ]
[Exit Do]
[ statements ]
Loop [{While | Until} condition ]
Search JabSto ::




Custom Search