Page 96 - Excel Progamming Weekend Crash Course
P. 96

h540629 ch05.qxd  9/2/03  9:33 AM  Page 71




                  Session 5 — Operators                                                   71


               String Operators

               There is a single string operator, the concatenation operator represented by the symbol &.
               Concatenation means simply putting two strings together. Here’s an example:

                  Dim s1 As String, s2 As String, s3 As String
                  s1 = “Micro”
                  s2 = “soft”
                  s3 = s1 & s2
                  The first string, s1, contains the text “Micro,” and the second string, s2, contains the
               text “soft.” When you concatenate, or combine, them, the result is that s3 contains
               “Microsoft.”
                          Sometimes the + symbol is used for concatenation. This is a holdover from
                          earlier versions of VBA and should not be used in your programs. It works
                  Never   fine, but it is better to use the & symbol to make your code as clear as
                          possible.
                  Even though it has only one string operator, VBA has extremely powerful text-handling
               capabilities.
                          VBA’s text-handling capabilities are covered in Session 10.

                 Cross-Ref



               Logical Operators
               Logical operators work with logical (True/False) values. With one exception, these operators
               combine two logical values into a single True/False value. You see examples of this on a reg-
               ular basis in daily life. For example, suppose you are shopping for a TV set and have the fol-
               lowing criteria:
                   At least 36-inch screen
                   Stereo sound
                   Costs less than $500
                  Your decision whether to buy a particular model could be phrased logically as follows:

                  Buy TV = (36” screen?) and (stereo sound?) and (less than $500?)
                  Here, you are using the And operator to say, in essence, that you will buy the TV if and
               only if all the conditions are True.
                  VBA has six logical operators, as summarized in Table 5-1.
   91   92   93   94   95   96   97   98   99   100   101