|
|
VBA provides a full complement of operators—items used for comparing, combining, and otherwise working with values. The operators fall into four categories:
Arithmetic operators (such as + for addition and / for division) for mathematical operations
Logical operators (such as Or) for building logical structures
Comparison operators (such as = and >=) for comparing values
String operators (such as & for joining two strings)
Table 26.2 lists the operators in their categories, with brief examples of each and comments on the operators that are not self-explanatory.
| Operator | Meaning | Example | Comments |
|---|---|---|---|
| Arithmetic Operators | |||
| Subtraction | x = y 1 | ||
| Negation | x | ||
| + | Addition | x = y + 1 | |
| * | Multiplication | x = y * 2 | |
| / | Division | x = y / 2 | |
| \ | Integer division (Div) | x = y \ 2 | Integer division truncates the remainder. |
| ^ | Exponentiation | x = y ^ 2 | |
| Mod | Modulo | 53 Mod 12 | Modulo returns the “remainder” part of a division operation. |
| And | Conjunction | If x > 1 And x < 2 | Both conditions must be True for a True result. |
| Not | Negation | If not x > 1 | |
| Or | Disjunction | If x > 100 Or X < 20 | If either condition is True, the result is True. |
| XOr | Exclusion | If x > 100 X Or y > 100 | If one condition is True and the other is False, the result is True; the result is False if both condi-tions are True or both are False. |
| Eqv | Equivalence | If x Eqv y | If both are True or both are False, the result is True. |
| Imp | Implication | If x Imp y | True if both values are True or the second comparison is True. |
| Comparison Operators | |||
| = | Equality | If x = y | |
| <> | Inequality | If intTest <> 55 | |
| < | Less than | If x < 10 | |
| > | Greater than | If x > 20 | |
| <= | Less than or equal to | If x <= 9 | |
| >= | Greater than or equalto | If x >= 19 | |
| Is | Object equivalence | If x is y | |
| String Operators | |||
| & | Concatenation | strA & strB | |
| + | Concatenation | strA + strB | Used less often due to potential confusion with addition. |
|
|