Excel VBA Comparison Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft’s programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend.
Some helpful links to get more insights about Macros, VBA in Excel :
2. How to Create a Macro in Excel?
In this article, we are going to discuss various comparison operators in Excel VBA.
Implementation :
In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available.
The Developer Tab can be enabled easily by a two-step process :
- Right-click on any of the existing tabs in the top of the Excel window.
- Now select Customize the Ribbon from the pop-down menu.
- The Excel Options Box, check the box Developer to enable it and click on OK.
- Now, the Developer Tab is visible.
- Now click on the Visual Basic option in the Developer tab and make a new module to write the program.
Developer -> Visual Basic -> Tools -> Macros
Now create a Macro and give any suitable name.
This will open the Editor window where can write the code.
Comparison Operators in Excel:
S.No. | Operators | Definition |
---|---|---|
1 | <> | Not equal operator is used to compare two operands. If the two operands are not equal it returns TRUE else it returns FALSE. For example : A=10, B= 20 The condition will be TRUE for A <> B, since A and B are not equal. |
2 | = | Equal operator is used to compare two operands. If the two operands are equal it returns TRUE else it returns FALSE. For example : A=20, B= 20 The condition will be TRUE for A = B, since A and B are equal. |
3 | > | Greater than operator checks whether the operand on the left hand side is strictly greater than the operand on RHS. If greater then it returns TRUE else FALSE. For example : A=10, B= 5 The condition will be TRUE for A > B, since A is greater than B. |
4 | < | Less than operator checks whether the operand on the left hand side is strictly less than the operand on RHS. If greater then it returns TRUE else FALSE. For example : A=10, B= 5 The condition will be FALSE for A < B, since A is greater than B. |
5 | >= | Greater than equal to operator checks whether the operand on the left hand side is either greater or equal to the operand on RHS. If greater or equal then it returns TRUE else FALSE. For example : A=10, B=10 The condition will be TRUE for A >= B, since A is equal to B. |
6 | <= | Less than equal to operator checks whether the operand on the left hand side is either lesser or equal to the operand on RHS. If lesser or equal then it returns TRUE else FALSE. For example : A=5, B=10 The condition will be TRUE for A <= B, since A is less than B. |
The comparison operators are mostly used with the If Else Then statement in Excel because comparison operators return TRUE if the condition is met and FALSE if not.
The syntax of If Else in Excel is :
If condition/expression Then Code Block for True Else Code Block for False End If
Let’s take an example where values of A=-1 and B=-5 and see the code in Excel VBA for all the comparison operators.
1. Equal To and Not Equal To
Sub Comparison_Operator_Demo() 'Entering the numbers Dim A As Integer: A = -1 Dim B As Integer: B = -5 'Condition for Equal To If A = B Then MsgBox " A and B are equal" Else MsgBox " A and B are not equal" End If End Sub
In the above code, If the condition becomes FALSE since A and B values are not the same. So the code block inside Else will be executed.
Sub Comparison_Operator_Demo() 'Entering the numbers Dim A As Integer: A = -1 Dim B As Integer: B = -5 'Condition for Not Equal To If A <> B Then MsgBox " True since A and B are not same" Else MsgBox " False since A and B are same" End If End Sub
In the above code If the condition becomes TRUE since A and B are not same. So the code block inside If will be executed.
2. Greater than or Less than operator :
Sub Comparison_Operator_Demo() 'Entering the numbers Dim A As Integer: A = -1 Dim B As Integer: B = -5 'Condition for Greater Than If A > B Then MsgBox " A is greater than B" Else MsgBox " A is not greater than B" End If End Sub
In the above code If the condition becomes TRUE since A is greater than B. So the code block inside If will be executed.
Sub Comparison_Operator_Demo() 'Entering the numbers Dim A As Integer: A = -1 Dim B As Integer: B = -5 'Condition for Less Than If A < B Then MsgBox "True because A is less than B" Else MsgBox "False because A is greater than B" End If End Sub
In the above code If the condition becomes FALSE since A is greater than B. So the code block inside Else will be executed.
Similarly, you can do for Greater than equal to and Less than equal to operators.
Attention reader! Don’t stop learning now. If you are an Excel beginner (or an intermediate) and want to learn Excel, Geeksforgeeks brings the perfect course for you to start, Diving Into Excel