VBA Comparison Operators – Not Equal to & More
VBA allows you to use comparison operators to compare values. By using these operators, you can compare values and return a Boolean True or False as a result.
The List of the Main Comparison Operators in VBA
First, we will display the list of comparison operators that can be used in VBA code:
Comparison Operator |
Explanation |
= |
Equal to |
<> |
Not Equal to |
> |
Greater than |
>= |
Greater than or Equal to |
< |
Less than |
<= |
Less than or Equal to |
Using the Comparison Operators to Compare Vales
The Equal to operator checks if two values are equal and returns True or False. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 |
Dim intA As Integer Dim intB As Integer Dim blnResult As Boolean intA = 5 intB = 5 If intA = intB Then blnResult = True Else blnResult = False End If |
In this example, we want to check if intA is equal to intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
First, we set values of intA and intB to 5:
1 2 |
intA = 5 intB = 5 |
After that, we use the = operator in the If statement to check if the values of intA and intB are equal:
1 2 3 4 5 |
If intA = intB Then blnResult = True Else blnResult = False End If |
Both variables are equal to 5, therefore the blnResult returns True:
Image 1. Using the Equal To operator
The Not Equal to operator checks if two values are not equal and returns True or False. Here is the example code:
An error has occurred. Please try again later. |
In this example, we want to check if intA is not equal to intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
We use the <> operator in the If statement to check if the values of intA and intB are different:
1 2 3 4 5 |
If intA <> intB Then blnResult = True Else blnResult = False End If |
The value of intA is 5 and the value of intB is 6, the variables are not equal, therefore the blnResult returns True:
Image 2. Using the Not Equal To operator
The Greater than operator checks if the first value is greater than the second value and returns True or False. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 |
Dim intA As Integer Dim intB As Integer Dim blnResult As Boolean intA = 6 intB = 5 If intA > intB Then blnResult = True Else blnResult = False End If |
In this example, we want to check if intA is greater than intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
Furthermore, we use the > operator in the If statement in order to check if the value of intA is greater than intB:
1 2 3 4 5 |
If intA > intB Then blnResult = True Else blnResult = False End If |
In conclusion, as intA is greater than intB, the blnResult returns True:
Image 3. Using the Greater Than operator
The Greater than or Equal to operator checks if the first value is greater than or equal to the second value and returns True or False. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 |
Dim intA As Integer Dim intB As Integer Dim blnResult As Boolean intA = 5 intB = 5 If intA >= intB Then blnResult = True Else blnResult = False End If |
In this example, we want to check if intA is greater than or equal to intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
We use the >= operator in the If statement to check if the value of intA is greater than or equal to intB:
1 2 3 4 5 |
If intA >= intB Then blnResult = True Else blnResult = False End If |
In conclusion, as both variables are equal to 5, the blnResult returns True:
Image 4. Using the Greater Than or Equal To operator
The Less than operator checks if the first value is less than the second value and returns True or False. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 |
Dim intA As Integer Dim intB As Integer Dim blnResult As Boolean intA = 5 intB = 6 If intA < intB Then blnResult = True Else blnResult = False End If |
In this example, we want to check if intA is less than intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
We use the < operator in the If statement to check if the value of intA is less than intB:
1 2 3 4 5 |
If intA < intB Then blnResult = True Else blnResult = False End If |
As intA is less than intB, the blnResult returns True:
Image 5. Using the Less Than operator
The Less than or Equal to operator checks if the first value is less than or equal to and returns True or False. Here is the example code:
1 2 3 4 5 6 7 8 9 10 11 12 |
Dim intA As Integer Dim intB As Integer Dim blnResult As Boolean intA = 5 intB = 5 If intA <= intB Then blnResult = True Else blnResult = False End If |
In this example, we want to check if intA is less than or equal to intB. If this is true, the value of Boolean blnResult will be True, otherwise, it will be False.
We use the <= operator in the If statement to check if the value of intA is less than or equal to intB:
1 2 3 4 5 |
If intA <= intB Then blnResult = True Else blnResult = False End If |
In conclusion, as both variables are equal to 5, the blnResult returns True:
Image 6. Using the Less Than or Equal To operator
If you want to learn how to compare strings, click here: VBA Compare Strings – StrComp
If you want to learn how to use comparison operators, click here: VBA Logical Operators