Page 111 - Excel Progamming Weekend Crash Course
P. 111

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




                86                                                          Saturday Morning

                  My advice is to avoid the Goto statement altogether (except, as you’ll see later in the
               book, in error-handling code). Its use can result in confusing and buggy code, and I have
               yet to see a programming situation that could not be handled better by VBA’s other control
               constructs.


                                                   REVIEW
               The VBA language provides a number of control constructs that let you modify the execu-
               tion of code in your program. You’ll find that these statements are important in all but the
               simplest Excel programs. In this session, you learned:
                   The If and Select Case statements are used to execute, or not execute, specified
                   statements depending on conditions in the program.
                   The For...Next statement lets you repeat execution a specified number of times.
                   The Do...Loop statement repeats execution while, or until, a specified condition
                   is met.
                   The For Each...Next statement is used to iterate through all the members of a
                   collection or all the elements of an array.
                   You can jump to another program location with Goto, but this statement is best
                   avoided.



                                               QUIZ YOURSELF
                 1. When would you want to use a Select Case statement in preference to an If
                    statement? (See the “The If...Then Statement” section.)
                 2. How would you use an If statement to execute code when a condition is False?
                    (Hint: there’s more than one way to do this.) (See the “The If...Then Statement”
                    section.)
                 3. How would you ensure that the code in a Do...Loop construct is executed at least
                    once? (See the “The Do...Loop Statement” section.)
                 4. Describe two ways to iterate through all the elements in an array. (See the “The
                    For...Next Statement” and “The For Each...Next Statement” sections.)
                 5. When should you use the Goto statement? (See the “The Goto Statement” section.)
   106   107   108   109   110   111   112   113   114   115   116