Skip to Content
Course content

2.2 Operators: Arithmetic, Comparison, and Logical.

In VBA, operators are symbols that perform operations on variables and values. Operators allow you to manipulate data, compare values, and control the flow of execution in your code. There are three primary types of operators in VBA: Arithmetic Operators, Comparison Operators, and Logical Operators.

2.2.1 Arithmetic Operators

Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, and division. These operators work on numeric data types (such as Integer, Double, etc.).

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 5 / 3 1.66667
\ Integer Division (returns quotient) 5 \ 3 1
Mod Modulo (returns remainder) 5 Mod 3 2

Example:

Sub ArithmeticExample()
    Dim result As Double
    result = 10 * 2 + 5 / 2
    MsgBox result  ' Output: 25
End Sub

In this example, VBA performs multiplication and division, following the order of operations (PEMDAS).

2.2.2 Comparison Operators

Comparison operators are used to compare two values. These operators return a Boolean result (True or False) based on the outcome of the comparison.

Operator Description Example Result
= Equal to 5 = 3 False
<> Not equal to 5 <> 3 True
< Less than 5 < 3 False
> Greater than 5 > 3 True
<= Less than or equal to 5 <= 3 False
>= Greater than or equal to 5 >= 3 True

Example:

Sub ComparisonExample()
    Dim result As Boolean
    result = (10 > 5)
    MsgBox result  ' Output: True
End Sub

In this example, the code compares whether 10 is greater than 5 and returns True.

2.2.3 Logical Operators

Logical operators are used to combine multiple conditions or expressions and return a Boolean value (True or False). These operators are often used in If...Then...Else statements and loops to control the flow of execution.

Operator Description Example Result
And Returns True if both conditions are True (5 > 3) And (8 < 10) True
Or Returns True if at least one condition is True (5 > 3) Or (8 > 10) True
Not Reverses the Boolean value (negates) Not (5 > 3) False

Example:

Sub LogicalExample()
    Dim result As Boolean
    result = (5 > 3) And (8 < 10)
    MsgBox result  ' Output: True
End Sub

In this example, both conditions (5 > 3) and (8 < 10) are True, so the And operator returns True.

2.2.4 Combining Operators

You can combine arithmetic, comparison, and logical operators to create more complex expressions. In these cases, it's important to follow the order of precedence, which determines the order in which operators are evaluated.

Order of Precedence:

  1. Parentheses ()
  2. Arithmetic Operators *, /, Mod, +, -
  3. Comparison Operators =, <>, <, >, <=, >=
  4. Logical Operators Not, And, Or

Example:

Sub CombinedExample()
    Dim result As Boolean
    result = (10 + 5 > 12) And (15 Mod 4 = 3)
    MsgBox result  ' Output: True
End Sub

In this example:

  • First, 10 + 5 is calculated to 15.
  • Then, 15 > 12 is True.
  • Next, 15 Mod 4 is calculated to 3, and 3 = 3 is True.
  • Finally, the And operator combines the two True conditions to return True.

2.2.5 Practical Use Cases for Operators

  1. Mathematical Calculations:
    • Use arithmetic operators to calculate totals, averages, or apply formulas.
  2. Conditional Logic:
    • Use comparison and logical operators within If statements to evaluate conditions and make decisions based on the results.
  3. Loops and Flow Control:
    • Combine logical operators in loop conditions (like Do While or For loops) to execute code as long as certain conditions are met.

Example:

Sub DiscountCalculation()
    Dim price As Double
    Dim discount As Double
    price = 100
    discount = 0

    If price > 50 Then
        discount = 0.1  ' 10% discount
    End If
    
    MsgBox "Discount: " & discount * price
End Sub

In this example, the If condition checks if the price is greater than 50. If it is, a discount is applied.

Summary

  • Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, division, and modulo.
  • Comparison operators compare two values and return a Boolean result (True or False).
  • Logical operators combine multiple conditions and are used to control the flow of execution based on the results.
  • By understanding how and when to use these operators, you can perform complex calculations, make decisions, and control the execution of your VBA code efficiently.

Commenting is not enabled on this course.