Excel VBA Comparison Operators
Last Updated : 05 Jul, 2021
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 :
1. Record Macros 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.
Similar Reads
Excel VBA Concatenation 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. Concatenation means to join two or more data into a single data. There are various ways we
6 min read
SQL Comparison Operators
SQL Comparison Operators are used to compare two values and check if they meet the specific criteria. Some comparison operators are = Equal to, > Greater than , < Less than, etc. Comparison Operators in SQLThe below table shows all comparison operators in SQL : OperatorDescription=The SQL Equa
3 min read
C++ Comparison Operators
Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result. There are mainly 6 Comparison Operators namely: Greater than (>) : this operator checks whether operand1 is greater than operand2. If the result t
3 min read
Comparison Operators in LISP
In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the
4 min read
VBA Arithmetic Operators in Excel
Arithmetic Operators are the operators which perform mathematical operation or which perform any operation between two numbers like addition(+), subtraction(-), multiplication(*), division( / ), exponentiation(^), modulus(Mod), Bit-Shift operations. In this article, we will learn about the excel VBA
3 min read
What are Comparison Operators in R?
Comparison operators are fundamental tools in any programming language, allowing you to compare values and make decisions based on the results. In R Programming Language comparison operators are used to compare numeric, string, and other types of data. They return a logical value (TRUE or FALSE) bas
3 min read
JavaScript Comparison Operators
JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. 1. Equality Operator (==) The Equality operator is used to compare the equality of two operands. [GFGTABS] JavaScript // Illustration of (==) operator let x = 5; let y = '5'; // Che
5 min read
Comparison Operators in Solidity
Comparison Operators are used to compare two values. Solidity has the following types of comparison operators: Greater than: Greater than operator compares two operands. It evaluates to true when the left operand is greater than the right operand otherwise false. It is denoted by >.Lesser than: L
3 min read
Comparison Operators in Programming
Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional
10 min read
VBA Logical Operators in Excel
Logical operators are used for performing logical and arithmetic operations on a set of values or variables. VBA allows you to use the Logical operators AND, OR, NOT, and XOR to compare values. The operators are considered "Boolean" which means they return True or False as a result. In Excel VBA, lo
6 min read