Arithmetic Operators

VBA accepts seven different arithmetic operators. When a statement contains multiple arithmetic operators, VBA uses precedence order to determine how to evaluate the statement. For example, VBA always calculates exponents first. The only exeception to the precedence order are parentheses. When parentheses separate portions of a statement, VBA

evaluates the contents of the parentheses first, still using the precedence order. In the following statement, Val2 is added to Val3, and the sum is multiplied by Val1.

The following table lists arithmetic Operator Precedence Order:

OPERATOR

PRECEDENCE

PURPOSE

A

1

Raises the number before the operator to the power of the exponent. For example, 2A3 = 8

-

2

Denotes a negative value.

*

3

Multiplies to numerical values.

/

3

Divides two numerical values and returns the entire result, including any decimal places. For example, 5 / 2 = 2.5.

\

3

Divides two numerical values and returns the integer portion of the result. For example, 5 \ 2 = 2.

Mod

4

Divide two numerical values and returns the remainder. For example, 5 MOD 2 = 1.

+

5

Adds two numerical expressions.

-

5

Finds the difference between two numerical expressions by subtracting the second expression from the first.

Was this article helpful?

0 0

Post a comment