# VBA Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within VBA that will be explained below.

## Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out.

Operator | Description |
---|---|

+ | Adds two values together. |

– | Subtracts one value from another. |

* | Multiplies two values together. |

/ | Divides one value by another. |

Mod | Calculates the remainder after one value is divided by another. |

^ | The exponentiation operator raises one value to the power of another. |

The following examples show each of these operators in action.

' Variables to use in expressions. Dim a As Integer Dim b As Integer Dim c As Double a = 10 b = 20 c = 0 c = a + b ' c will equal 30. c = b - a ' c will equal 10. c = a * b ' c will equal 200. c = b / a ' c will equal 2. c = b Mod a ' c will equal 0. c = a ^ b ' c will equal 1E+20.

## Relational Operators

Relational operators compare two values and are used, for example, in ‘If’ statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator | Description |
---|---|

= | Evaluates to True if the two values are equal. |

<> | Evaluates to True if the two values are not equal. |

> | Evaluates to True if the first value is greater than the second. |

< | Evaluates to True if the first value is less than the second. |

>= | Evaluates to True if the first value is greater than or equal to the second. |

<= | Evaluates to True if the first value is less than or equal to the second. |

## Logical Operators

Logical operators are used where it is required to test more than one condition at the same time, for example, in an ‘If’ statement, which is discussed in the next section on Decision Making.

Operator | Description |
---|---|

AND | Logical AND operator where operands on both sides must be True to evaluate to True. |

OR | Logical OR operator where only one of the two operands must be True to evaluate to True. |

NOT | Logical NOT operator that can be used in conjunction with ‘AND’ or ‘OR’ to reverse the resulting operand. In the case of ‘AND’, if both conditions evaluate to True, then with the use of the ‘NOT’ operator the result would be False. |

XOR | Logical exclusion operator that evaluates to True if one and only one of the operands evaluates to True. |

## Concatenation Operator

The concatenation operator can be used to concatenate, or join, two values together to make one.

Operator | Description |
---|---|

& | Concatenates two values together. |