Page 106 - Microsoft Office Excel 2003 Programming Inside Out
P. 106

Microsoft Office Excel 2003 Programming Inside Out

                             As you can see in the following code, the syntax for the two loops is straightforward:

                             Do While condition
                             [statement]
                             [Exit Do]
                             [statement]
                             Loop
                             and

                             Do Until condition
                             [statement]
                             [Exit Do]
                             [statement]
                             Loop
                               ●  condition  A required numeric or string expression that must evaluate to True or False
                               ●  statement  One or more optional statement lines that are executed during the loop
                               ●  Exit Do  Optional statement to exit the loop prematurely
                               ●  Loop  Required statement to mark the end of the Do While or Do Until statement block
                             The Do While loop tests the condition first, before entering the loop, and executes while the
                             condition is true. This example performs a simple counting loop, similar to a For…Next
                             statement:

                             Dim intCounter as Integer
                             intCounter = 1
                             Do While intCounter <= 10
                                 intCounter = intCounter + 1
                             Loop
                             The following Do Until loop performs the same actions as the Do While loop, but notice how
             Chapter 4
                             the condition expression has changed:

                             Dim intCounter as Integer
                             intCounter = 1
                             Do Until intCounter = 11
                                 intCounter = intCounter + 1
                             Loop
                             Both examples run a simple counting loop from 1 to 10. In these two examples, it is easier to
                             figure out that the Do While loop is counting from 1 to 10. The Do Until loop appears to be
                             counting to 11, but the action is not performed once the counter reaches 11. It is last per-
                             formed with a value of 10.
                             The Do…Loop While and Do…Loop Until loops are similar to each other, just as they are sim­
                             ilar to the Do While and Do Until loops mentioned earlier. Both check the conditional expres­
                             sion at the end of the loop, guaranteeing at least one pass through the loop. Again, Do…Loop
                             While loops while the condition is true, and Do…Loop Until loops while the condition is
                             false. The syntax declarations for them are shown here:

                80
             Part 2:  Visual Basic for Applications
   101   102   103   104   105   106   107   108   109   110   111