Chapter 6: Operators


Overview

Operators perform mathematical functions, comparison functions, or logical operations between two numbers or numerical expressions within your program. A simple example of an operator is the plus (+) or minus ( ‚ ) sign. You will have already come across many operators when using spreadsheet formulas.

Operators have orders of precedence that determine the order in which the calculations take place. Within individual categories (arithmetic, comparison, and logical), operators are evaluated in the order of precedence as shown in the following table from the top down:

Arithmetic

Comparison

Logical

Exponentiation (^)

Equality (=)

Not

Negation ( ‚ )

Inequality (<>)

And

Multiplication and division (*, /)

Less than (<)

Or

Integer division (\)

Greater than (>)

Xor

Modulo arithmetic (Mod)

Less than or equal to (<=)

Eqv

Addition and subtraction (+, ‚ )

Greater than or equal to (>=)

Imp

String concatenation (&)

Like / Is

‚  

These orders of precedence can be changed by using brackets within the formula, in the same way that you can in Excel formulas. The formulas within the innermost nested set of brackets will always be evaluated first.

The use of brackets to change the order of precedence can end up giving different results than you expect, so it is important to understand how they work. Try the following code examples in a subroutine on a module:

 MsgBox (10 + 6) / 3 

This gives the answer 5.3333.

 MsgBox 10 + 6 / 3 

This gives the answer 12.

In the first example, the brackets force 10 + 6 to be evaluated first before division by 3. In the second example, the division of 6 / 3 takes precedence followed by the addition of 10.




Excel VBA Macro Programming
Excel VBA Macro Programming
ISBN: 0072231440
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net