Using DoLoop Structures

What do you do when you need to loop but you don't know in advance how many times to repeat the loop? This could happen if, for example, you want to loop only until a certain condition is met, such as encountering a blank cell in an Excel worksheet. The solution is to use a Do...Loop.

The Do...Loop has four different syntaxes:

Do While condition

[statements]

Loop

[statements]

Loop While condition

Checks condition before entering the loop. Executes the statements only while condition is True.

Checks condition after running through the loop once. Executes the statements only while condition is True. Use this form when you want the loop to be processed at least once.

Do Until condition [ statements] Loop

[ statements]

Loop Until condition

Checks condition before entering the loop. Executes the statements only while condition is False.

Checks condition after running through the loop once. Executes the statements only while condition is False. Again, use this form when you want the loop to be processed at least once.

Listing 6.11 shows a procedure called BigNumbers that runs down a worksheet column and changes the font color to magenta whenever a cell contains a number greater than or equal to 1,000.

0 0

Post a comment