Page 106 - Excel Progamming Weekend Crash Course
P. 106

h540629 ch06.qxd  9/2/03  9:33 AM  Page 81




                  Session 6 — Control Constructs                                          81

                  You can also use To and Is with strings.

                          Please refer back to Session 5 for details on how VBA performs string com-
                          parisons.
                 Cross-Ref



               The Do...Loop Statement
               The Do...Loop statement, or Do for short, is used to execute a block of statements
               repeatedly — in other words, to loop through the statements. For instance, suppose your
               program needed to look through a column of numbers in a worksheet until it found a value
               of zero — a Do...Loop statement would be the ideal way to write the required code. This
               statement comes in several variations that can meet essentially any program need. They are
               described in Table 6-1.


               Table 6-1 Variations on the Do Statement
               Syntax                 Action                   Notes
               Do While condition

               statements
               Loop                   Repeatedly executes      If condition is initially false,
                                      statements as long as    statements do not execute at all.
                                      condition remains true.
               Do Until condition

               statements
               Loop                   Repeatedly executes      If condition is initially true,
                                      statements until         statements will not be executed
                                      condition becomes true.  at all.
               Do

               statements
               Loop While condition   Executes statements once;  statements are always executed
                                      then repeats as long as   at least once.
                                      condition remains true.
               Do

               statements
               Loop Until condition   Executes statements      statements are always executed
                                      once; then repeats until   at least once.
                                      condition becomes true.
   101   102   103   104   105   106   107   108   109   110   111