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

VBA Programming Starter Kit

                             Table 4-11.  Comparison Operators
                             Operator     Description
                             =            Determines if two values are equal
                             <            Determines if value on left side of operand is less than value on right side
                             >            Determines if value on left side of operand is greater than value on right side
                             <=€          Determines if value on left side of operand is less than or equal to value on
                                          right side
                             >=	          Determines if value on left side of operand is greater than or equal to value
                                          on right side
                             <>           Determines if two values are not equal to each other

                             The following code fragments show examples of using the If…Then…Else statement to
                             determine if a person’s age allows them to vote:

                             If intAge >= 18 Then
                                 boolAllowVote = True
                             Else
                                 boolAllowVote = False
                             End If

                             If boolAllowVote Then
                                 [Display ballot and record vote]
                             End If

                             intReturn = MsgBox("Do you wish to continue?", vbYesNo + vbExclamation, "My App")
                             If intReturn = vbYes Then
                                 [Continue processing]                                                       Chapter 4
                             Else
                                 [Exit Procedure]
                             End If

                             Select Case Statements
                             Select Case statements allow you to check for multiple values at once. Suppose you had to cal­
                             culate different values depending upon what month it was. You would need to write eleven
                             If…Then…Else statements to check for all twelve months, but using a Select Case statement
                             lets you drop the number of conditional statements to one, making your code easier to read
                             and maintain.

                             The syntax of the Select Case statement is shown here.
                             Select [Case] testcondition
                                 [Case expressionlist
                                    [statements] ]
                                 [Case Else
                                    elsestatements
                             End Select

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