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

VBA Programming Starter Kit

                                        Else
                                            intDays = 28
                                        End If
                                    End If

                             This example also shows how you can mix If…Then…Else statements inside a Select Case
                             statement.


                    Loops
                             There will be numerous times when you’ll need to perform a certain task several times, so
                             VBA provides several methods of creating loops. Loops can be divided into two categories:
                             iteration loops and logical loops.


                             Iteration Loops
                             Iteration loops, which are used to ensure that a specific number of repetitions have occurred,
                             have a definitive starting point and ending point. There are two iteration loops, both similar
                             in style and syntax.
                             The first type of iteration loop, the For…Next loop, is normally used for counting and is par­
                             ticularly useful with arrays. The syntax of a For…Next loop is shown here.

                             For counter = start To end [Step step]
                                 [statements]
                                 [Exit For]
                                 [statements]
                             Next counter
                               ●	 counter  Required numeric variable that is used as a counter. It can’t be a Boolean or   Chapter 4
                                  member of an array.
                               ●  start  Required value used as the starting point of the array.
                               ●  end  Required value used as the ending point of the array.
                               ●	 step  Optional value by which the counter should be incremented during each itera­
                                  tion of the loop. Step value can be a positive or negative value. Default value is 1.
                               ●	 statements  One or more optional statement lines that are executed during each iter­
                                  ation of the loop.
                               ●	 Exit For  Optional statement used to exit the loop prematurely. Code execution
                                  resumes at the first line following the Next counter statement.
                               ●  Next counter  Required statement marking the end of the For…Next loop.

                             You can omit the counter variable used within the Next statement, but it isn’t recommended.
                             By including the counter, you add an extra level of protection against programming errors.







                                                                                                        77
                                                                                        Part 2:  Visual Basic for Applications
   98   99   100   101   102   103   104   105   106   107   108