Operators are symbols or keywords used to perform operations on values and variables in VBA. They are used for calculations, comparisons, logical decisions, and combining text.
Operators are an important part of VBA programming because they allow us to perform calculations and create conditions.
An operator is a symbol or keyword that tells VBA to perform an operation.
For example:
10 + 20
Here, + is an arithmetic operator used for addition.
VBA provides several categories of operators.
Each category is used for a different type of operation.
The + operator is used to add numbers.
Dim total As Integer
total = 10 + 20
MsgBox total
The result is 30.
The - operator is used to subtract one number from another.
Dim result As Integer
result = 50 - 20
MsgBox result
The result is 30.
The * operator is used for multiplication.
Dim result As Integer
result = 10 * 5
MsgBox result
The result is 50.
The / operator performs division and produces a division result that can include a decimal portion.
Dim result As Double
result = 10 / 4
MsgBox result
The result is 2.5.
The \ operator performs integer division. It returns the whole-number quotient.
Dim result As Integer
result = 10 \ 4
MsgBox result
The result is 2.
The Mod operator returns the remainder after division.
Dim remainder As Integer
remainder = 10 Mod 3
MsgBox remainder
The result is 1 because 10 divided by 3 leaves a remainder of 1.
The ^ operator is used to raise a number to a power.
Dim result As Double
result = 2 ^ 3
MsgBox result
The result is 8.
The main arithmetic operators in VBA are:
| Operator | Purpose | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| \ | Integer division | 10 \ 3 |
| Mod | Remainder | 10 Mod 3 |
| ^ | Exponentiation | 2 ^ 3 |
The = operator is used to assign a value to a variable.
Dim marks As Integer
marks = 85
Here, 85 is assigned to the variable marks.
The = symbol can also be used for comparison in a condition.
If marks = 50 Then
MsgBox "Marks are 50"
End If
In this condition, VBA checks whether marks is equal to 50.
The <> operator means not equal to.
If marks <> 0 Then
MsgBox "Marks are available"
End If
The condition is True when marks is not equal to 0.
The > operator checks whether one value is greater than another.
If marks > 40 Then
MsgBox "Above passing marks"
End If
The < operator checks whether one value is less than another.
If marks < 40 Then
MsgBox "Below passing marks"
End If
The >= operator checks whether a value is greater than or equal to another value.
If marks >= 40 Then
MsgBox "Pass"
End If
The condition is True when marks are 40 or more.
The <= operator checks whether a value is less than or equal to another value.
If marks <= 100 Then
MsgBox "Valid marks"
End If
Comparison operators are used to compare two values.
| Operator | Meaning |
|---|---|
| = | Equal to |
| <> | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
The And operator is used when all specified conditions must be True.
If marks >= 40 And attendance >= 75 Then
MsgBox "Eligible"
End If
Both conditions must be True for the complete condition to be True.
The Or operator is used when at least one of the specified conditions can be True.
If marks >= 80 Or attendance >= 90 Then
MsgBox "Eligible"
End If
The condition is True if either condition is True.
The Not operator reverses a logical value.
Dim passed As Boolean
passed = False
If Not passed Then
MsgBox "Student has not passed"
End If
If passed is False, Not passed becomes True.
Common logical operators in VBA include:
| Operator | Purpose |
|---|---|
| And | All specified conditions must be True |
| Or | At least one condition can be True |
| Not | Reverses a logical value |
Logical operators are especially useful with If statements.
The & operator is commonly used to join text values together.
Dim firstName As String
Dim lastName As String
Dim fullName As String
firstName = "Rahul"
lastName = "Kumar"
fullName = firstName & " " & lastName
MsgBox fullName
The result is:
Rahul Kumar
Operators can be used with variables instead of direct values.
Dim a As Integer
Dim b As Integer
Dim total As Integer
a = 25
b = 15
total = a + b
MsgBox total
The + operator adds the values stored in the variables.
Operators can also be used with values stored in Excel cells.
Sub CalculateTotal()
Range("C2").Value = Range("A2").Value + Range("B2").Value
End Sub
This adds the values in A2 and B2 and places the result in C2.
When an expression contains multiple arithmetic operators, VBA follows operator precedence rules.
For example:
Dim result As Double
result = 10 + 5 * 2
MsgBox result
Multiplication is performed before addition, so the result is 20.
Parentheses can be used when you want a specific part of an expression to be calculated first.
result = (10 + 5) * 2
The result is 30.
Operators are commonly used in student result systems.
Sub StudentResult()
Dim marks As Integer
Dim total As Integer
Dim percentage As Double
marks = 425
total = 500
percentage = marks / total * 100
If percentage >= 40 Then
MsgBox "Pass: " & percentage & "%"
Else
MsgBox "Fail: " & percentage & "%"
End If
End Sub
This example uses arithmetic, comparison, and concatenation operators.
Clear expressions make VBA programs easier to read and maintain.
Operators allow VBA to perform calculations, compare values, combine text, and create logical conditions.
The major groups include arithmetic operators such as +, -, *, /, \, Mod, and ^; comparison operators such as =, <>, >, <, >=, and <=; logical operators such as And, Or, and Not; and the & operator for text concatenation.
Understanding operators is essential for writing conditions, calculations, student result systems, data-processing programs, and practical Excel VBA applications.
Question: Which VBA operator returns the remainder after division?