VBA Operators

Updated on January 1, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

Excel VBA Operators

In VBA, operators check whether one number is greater than another, less than another, equal to another, and not equal. The method is similar to the operators we use in Excel, such as A>B using a comparison operator.

It does not matter how good or proficient we are at our work. However, everything will be messy if we do not do the basics correctly. Firstly if we do not learn the basics right, then we cannot progress to the next level, be it any profession. We are pressing so much on basics because, in today’s article, we will show you one of the basic concepts, “VBA Operators.”

Operators are the heart of any calculation. They are the signs we use to compare one thing with another. We are sure you must have used this logic in your daily workplace.

VBA Operators

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Operators (wallstreetmojo.com)

List of Mathematical Operators

Below is the mathematical operator’s list we use regularly.

vba operators list

Above are the mathematical operators and those are common to everybody. We have comparison operators as well. Below is the list of those.

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

List of Comparison Operators for VBA

Let’s discuss these operators in detail.

You can download this VBA Operators Excel Template here – VBA Operators Excel Template

Equal Sign (=)

One may use this sign to compare whether one thing is equal to another. The result of this operator sign is either TRUE or FALSE. If one thing equals another, we will get TRUE or else FALSE.

Below is the VBA Code to understand the use of the Equal(=) operator.

Code:

Sub Equal_Operator()

Dim Val1 As String
Dim Val2 As String

Val1 = 25
Val2 = 25

If Val1 = Val2 Then
MsgBox "Both are same and result is TRUE"
Else
MsgBox "Both are not same and result is FALSE"
End If

End Sub
vba operators example 1.1

It will return the result as TRUE because variable values “Val1” and “Val2” are the same.

vba operators example 1.2

Greater Than Sign (>)

This sign checks whether one number is greater than the other number. It is also a logical VBA operator where the result is either TRUE or FALSE.

Below is the VBA CodeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more to understand the use of the Greater Than (>) operator.

Code:

Sub Greater_Operator()

Dim Val1 As String
Dim Val2 As String

Val1 = 25
Val2 = 20

If Val1 > Val2 Then
MsgBox "Val1 is greater than the val2 and result is TRUE"
Else
MsgBox "Val1 is not greater than the val2 and result is FALSE"
End If

End Sub
vba operators example 2.1

And the result will be:

vba operators example 2.2

Greater Than or Equal to Sign (>=)

This sign works the same as the above operator is greater than but checks whether the number is equal or not.

Below is the VBA Code to understand the use of the Greater Than or Equal to(>=) operator.

Code:

Sub Greater_Than_Equal_Operator()

Dim Val1 As String
Dim Val2 As String

Val1 = 25
Val2 = 20

If Val1 >= Val2 Then
MsgBox "Val1 is greater than the val2 and result is TRUE"
Else
MsgBox "Val1 is not greater than the val2 and result is FALSE"
End If

End Sub
vba operators example 3.1

Now, we will change the Val2 amount to 25 and run the code.

example 3.2

Both the results return TRUE because we have applied >= sign.

example 3.3

Less than Sign (<)

This sign checks whether one number is less than the other number. It is also a logical operator in VBA, where the result is either TRUE or FALSE.

Below is the VBA Code to understand the use of the Less Than (<) operator.

Code:

Sub Less_Operator()

Dim Val1 As String
Dim Val2 As String

Val1 = 25
Val2 = 20

If Val1 < Val2 Then
MsgBox "Val1 is less than the val2 and result is TRUE"
Else
MsgBox "Val1 is not less than the val2 and result is FALSE"
End If

End Sub
example 4.1

It returns FALSE because 25 is not less than 20.

example 4.2

Not Equal to Sign (<>)

It is not equal to sign if the inverse operator returns inverse results. If one thing is equal to another, it returns FALSE or TRUE.

Below is the code to understand the use of the VBA Not Equal (<>) operator.

Code:

Sub NotEqual_Operator()

Dim Val1 As String
Dim Val2 As String

Val1 = 25
Val2 = 20

If Val1 <> Val2 Then
MsgBox "Val1 is not equal to val2 and result is TRUE"
Else
MsgBox "Val1 is equal to val2 and result is FALSE"
End If

End Sub
example 5.1

You will get the following output.

example 5.2

Recommended Articles

This article is a guide to VBA Operators. Here, we provide the list of mathematical and comparison operators, practical examples of VBA, and a downloadable Excel template. You can learn more about VBA from the following articles: –