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

Microsoft Office Excel 2003 Programming Inside Out

                               ●	 testcondition  Required expression that must evaluate to one of the basic data types,
                                  such as Boolean, Integer, String, and so on.
                               ●	 expressionlist  A list of expression clauses representing values for testexpression.
                                  Multiple expression clauses are separated by commas and can take one of the following
                                  forms:
                               ●	 Expression1 To Expression2  Used to represent a range of values from Expression1 to
                                  Expression2. Expression1 must be less than Expression2.
                               ●	 [Is] comparisonoperator Expression  comparisonoperator is used to specify a restric­
                                  tion on the value of Expression.
                               ●	 Expression  The expressions in expressionlist can be any data type so long as they are
                                  implicitly convertible to the type of test condition.
                               ●	 Statements  One or more statements that are executed if the testexpression matches
                                  any clause in expressionlist.
                               ●	 else statements  One or more statements that are executed if the testexpression does
                                  not match any clause in expressionlist.
                               ●  End Select  Required to mark the end of the Select Case block.
                             The following code fragment demonstrates using the Select Case statement to set a variable to
                             the number of days in each month.

                             Select Case strMonth
                                 Case "February"
                                    intDays = 28
                                 Case "April", "June", "September", "November"
                                    intDays = 30

                                 Case "January", "March", "May", "July", "August", "October", "December"
                                    intDays = 31
             Chapter 4
                             End Select
                             This preceding code is just a simple example of specifying the number of days in a month, but
                             does not make any provisions for leap years. More code can be added to the “February”
                             clause to properly set the number of days in February, as in the following code:

                                 Case "February"
                                    If (intYear Mod 100) = 0 Then
                                        If (intYear Mod 400) = 0 Then
                                            intDays = 29
                                        Else
                                            intDays = 28
                                        End If
                                    Else
                                        If (intYear Mod 4) = 0 Then
                                            intDays = 29




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