VBA Operators

Excel VBA Operators

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

It doesn’t matter how good we are or how proficient we are at our work. If we don’t do the basics right, then everything will be in a mess. Firstly if we don’t learn the basics right, then we cannot progress to the next level, be it any profession. The reason why I am 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. I am sure you must have used these logics 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.

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 (=)

This sign is used to compare whether one thing is equal to another thing. The result of this operator sign is either TRUE or FALSE. If the one thing is equal to another, then we will get TRUE or else FALSE.

Below is the VBA Code to understand the use of 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

This will return the result as TRUE because variables values “Val1” & “Val2” are the same.

vba operators example 1.2

Greater Than Sign (>)

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

Below is the VBA Code to understand the use of 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 exactly the same as the above operator Greater Than but checks whether the number is equal or not.

Below is the VBA Code to understand the use of 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 just change the val2 amount to 25 and then 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. This 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

This returns FALSE because 25 is not less than 20.

example 4.2

Not Equal to Sign (<>)

This not equal to sign is the inverse operator returns inverse results. If the one thing is equal to another, then it returns FALSE or else 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 has been a guide to VBA Operators. Here we provide the list of mathematical and comparison operators along with practical examples of VBA and a downloadable excel template. You can learn more about VBA from the following articles –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>