## Other operators

Operators play a major role in VBA. Besides the equal sign operator (discussed in the previous section), VBA provides several other operators. Table 7-3 lists these operators, with which you are familiar from your worksheet formulas experience.

Table 7-3

VBA's Operators

Table 7-3

Function |
Operator Symbol |

Addition |
+ |

Multiplication |
* |

Division |
/ |

Subtraction - | |

Exponentiation |
A |

String concatenation |
& |

Integer division (the result is | |

always an integer) |
\ |

Modulo arithmetic (returns the | |

remainder of a division operation) |
Mod |

The term concatenation is programmer speak for "put together." Thus, if you concatenate strings, you are combining strings to make a new and improved string.

The term concatenation is programmer speak for "put together." Thus, if you concatenate strings, you are combining strings to make a new and improved string.

As shown in Table 7-4, VBA also provides a full set of logical operators. Consult the Help system for complete details.

Table 7-4 VBA's Logical Operators

Operator What It Does

Not Performs a logical negation on an expression

And Performs a logical conjunction on two expressions

Or Performs a logical disjunction on two expressions

XoR Performs a logical exclusion on two expressions

Eqv Performs a logical equivalence on two expressions

Imp Performs a logical implication on two expressions -TEAM LinG

The precedence order for operators in VBA is exactly the same as in Excel formulas. Exponentiation has the highest precedence. Multiplication and division come next, followed by addition and subtraction. You can use parentheses to change the natural precedence order, making whatever's sandwiched in parentheses come before any operator. Take a look at this code:

The value of z is 13, not 16. That's because the multiplication operation (5 * y) is performed first, and that result is added to x. To avoid ambiguity, I prefer to use parentheses even when they aren't required. For example, I would write that last assignment statement like this:

Don't be shy about using parentheses even if they aren't required — especially if doing so makes your code easier to understand. VBA doesn't care if you use extra parentheses.

## Post a comment