Skip to Content
Course content

2.2. Operators and Expressions

In VBA (Visual Basic for Applications), operators are symbols or keywords used to perform operations on variables, constants, or values. Expressions are combinations of variables, constants, operators, and functions that are evaluated to produce a result. Understanding operators and expressions is fundamental to performing calculations, comparisons, and logical operations in your code.

1. Types of Operators in VBA

VBA provides several types of operators that you can use to perform different kinds of operations, such as arithmetic, comparison, logical, and concatenation.

1.1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

Operator Description Example
+ Addition result = 5 + 3
- Subtraction result = 5 - 3
* Multiplication result = 5 * 3
/ Division result = 5 / 3
\ Integer Division result = 5 \ 3
Mod Remainder (Modulus) result = 5 Mod 3
^ Exponentiation result = 5 ^ 2
  • Example:
    Dim result As Double
    result = 10 + 5 * 2  ' Result = 20
    

1.2. Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (True or False).

Operator Description Example
= Equal to result = (5 = 5)
<> Not equal to result = (5 <> 3)
> Greater than result = (5 > 3)
< Less than result = (5 < 3)
>= Greater than or equal to result = (5 >= 3)
<= Less than or equal to result = (5 <= 3)
  • Example:
    Dim result As Boolean
    result = (10 > 5)  ' Result = True
    

1.3. Logical Operators

Logical operators are used to combine multiple conditions in logical expressions. They return True or False.

Operator Description Example
And Returns True if both expressions are True result = (5 > 3 And 10 > 5)
Or Returns True if at least one expression is True result = (5 > 3 Or 3 > 5)
Not Reverses the logical value result = Not (5 > 3)
  • Example:
    Dim result As Boolean
    result = (5 > 3 And 10 > 5)  ' Result = True
    

1.4. Concatenation Operator

The concatenation operator (&) is used to join two or more strings together.

Operator Description Example
& Concatenation (joining strings) result = "Hello" & " " & "World"
  • Example:
    Dim result As String
    result = "Hello" & " " & "World"  ' Result = "Hello World"
    

1.5. Unary Operators

Unary operators operate on a single operand, typically modifying or negating its value.

Operator Description Example
+ Unary plus (positive value) result = +5
- Unary minus (negates value) result = -5
Not Negates a Boolean value result = Not True
  • Example:
    Dim result As Boolean
    result = Not True  ' Result = False
    

2. Expressions in VBA

An expression is a combination of variables, constants, operators, and functions that are evaluated to produce a result. Expressions are used extensively in VBA to perform calculations, make decisions, and control program flow.

2.1. Simple Expressions

A simple expression might just involve a variable and a constant, or a constant and an operator. For example:

Dim result As Integer
result = 10 + 5  ' Simple expression evaluating to 15

2.2. Complex Expressions

Complex expressions involve multiple variables and operators. For example:

Dim total As Double
Dim price As Double
Dim quantity As Integer
price = 19.99
quantity = 5
total = price * quantity + 10  ' Complex expression

2.3. Using Functions in Expressions

You can also use functions within expressions. For example:

Dim currentDate As Date
Dim year As Integer
currentDate = Now
year = Year(currentDate)  ' Using the Year function in an expression

2.4. Nested Expressions

Expressions can also be nested, meaning that one expression can be used within another. For example:

Dim result As Double
result = (10 + 5) * (3 - 1)  ' Nested expressions

3. Operator Precedence in VBA

When multiple operators are used in an expression, operator precedence determines the order in which they are evaluated. For example, multiplication and division have higher precedence than addition and subtraction.

Operator Precedence
^ (Exponentiation) Highest
*, /, \, Mod Medium
+, - Low
=, <>, >, <, >=, <= Lowest

Example of Operator Precedence:

Dim result As Double
result = 10 + 5 * 2  ' Multiplication happens first, result = 10 + 10 = 20

To alter the precedence of operators, you can use parentheses. Parentheses have the highest precedence and ensure that expressions inside them are evaluated first.

Dim result As Double
result = (10 + 5) * 2  ' Addition happens first, result = 15 * 2 = 30

4. Error Handling in Expressions

Sometimes, expressions may result in errors, such as division by zero or invalid operations. It’s important to handle errors to ensure smooth execution of your program.

Dim result As Double
On Error Resume Next
result = 10 / 0  ' Division by zero won't cause an error, result will be empty
If Err.Number <> 0 Then
    MsgBox "An error occurred"
End If

Conclusion

Operators and expressions are the foundation of any VBA program, enabling you to perform calculations, comparisons, logical decisions, and string manipulations. By understanding the different types of operators (arithmetic, comparison, logical, and concatenation), as well as how to use expressions effectively, you can write more efficient and powerful VBA code. Operator precedence and error handling are also essential concepts for ensuring that your expressions work as expected and your program runs smoothly.

Commenting is not enabled on this course.