The Order of Precedence

The order of precedence that VBA uses is determined by the various expression operators I outlined in the preceding section. Table 4.4 summarizes the complete order of precedence used by VBA.

Table 4.4 The VBA Order of Precedence

Operator

Operation

Order of Precedence

A

Exponentiation

First

Negation

Second

* and /

Multiplication and division

Third

\

Integer division

Fourth

Mod

Modulus

Fifth

+ and —

Addition and subtraction

Sixth

&

Concatenation

Seventh

= < > <= >= <>

Comparison

Eighth

And Eqv Imp Or Xor Not

Logical

Ninth

From this table, you can see that VBA performs exponentiation before addition. Therefore, the correct answer for the expression 3+5A2 (just discussed) is 28.

Notice, as well, that some operators in Table 4.4 have the same order of precedence (for example, multiplication and division). This means that it doesn't matter in which order these operators are evaluated. For example, consider the expression 5*10/2. If you perform the multiplication first, the answer you get is 25 (5*10 equals 50, and 50/2 equals 25). If you perform the division first, you also get an answer of 25 (10/2 equals 5, and 5*5 equals 25). By convention, VBA evaluates operators with the same order of precedence from left to right.

0 0

Post a comment