Page 95 - Excel Progamming Weekend Crash Course
P. 95

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




                70                                                          Saturday Morning


                   The item on the left side of the assignment operator must be some element that
                   can be assigned a value — that is, a variable, property or, in a Const statement,
                   a constant.
                   When assigning an object reference, the line must begin with the Set keyword.
                   The item on the right side of the assignment operator must be an expression —
                   anything that evaluates to a value.


                          The term expression means any VBA code that can be reduced, or evaluated,
                          to a single numerical or string value. Thus, 5+2-1 is an expression because
                   Note   it can be evaluated to the single value 6. Likewise “Micro” & “soft” is an
                          expression because it evaluates to the single value “Microsoft.”
                  There are some restrictions regarding the data types of the two elements in an assign-
               ment statement.
                   If you assign a string value to a numeric variable, any value other than True or False
                   to a Boolean variable, or any nonobject value to an object variable, an error occurs.
                   If you assign a value to a numeric variable that is out of the range for that type,
                   an error occurs.
                   If you assign a number value to a string variable, the number is converted to the
                   text equivalent.
                   If you assign a floating-point value to an integer variable, the value is rounded to
                   the nearest integer.

                  The = symbol is also used by VBA as a comparison operator, as explained later in this ses-
               sion. It is always clear from the context whether this symbol is being used for assignment
               or comparison.

                          The concept of an expression is important in programming. An expression
                          is anything that can be evaluated to a value. It can be a numeric value,
                   Note   a string, an object reference, or a logical (True/False) value.



               Numerical Operators
               The numerical operators, sometimes called the mathematical operators, perform operations
               with numbers. The familiar operations of addition, subtraction, multiplication, and division
               are represented by the symbols +, -, *, and / respectively. There are three less commonly
               used operations:

                   Integer division (\). Performs a division, and returns the integer part of the
                   answer; therefore, 11\3 evaluates to 3.
                   Exponentiation (^). Raises a number to a power. In VBA, 4^2 means “four to the
                                   2
                   second power” or 4 , and evaluates to 16.
                   Modulus (mod). Performs a division and returns the remainder. For example, 18 mod
                   5 evaluates to 3. Floating-point numbers are rounded to integers when used with
                   mod.
   90   91   92   93   94   95   96   97   98   99   100