Excel VBA Comparison Operators
Learn how to compare numbers, text, dates and expressions and make decisions in Excel VBA.
Introduction to Comparison Operators
Comparison operators are used in VBA to compare two values, variables or expressions.
The result of a comparison is generally a logical result: True or False. VBA also has situations where a comparison can result in Null, particularly when a Variant expression contains Null.
Comparison operators are extremely important because they allow VBA to make decisions.
Suppose:
Age = 25
We can ask VBA:
Is Age greater than or equal to 18?
VBA evaluates:
25 >= 18
Result: True
Why Do We Use Comparison Operators?
Comparison operators are used whenever a VBA program needs to determine whether one value is related to another value in a particular way.
They are commonly used for:
- Checking whether two values are equal.
- Checking whether a value is greater or smaller.
- Validating user input.
- Checking employee salary or age.
- Checking sales targets.
- Comparing dates.
- Filtering Excel data.
- Controlling If...Then statements.
- Controlling loops.
- Combining multiple conditions with logical operators.
Comparison Operators in VBA
| Operator | Name | Example | Meaning |
|---|---|---|---|
| = | Equal To | 10 = 10 | Both values are equal |
| <> | Not Equal To | 10 <> 5 | Values are different |
| > | Greater Than | 10 > 5 | Left value is greater |
| < | Less Than | 5 < 10 | Left value is smaller |
| >= | Greater Than or Equal To | 10 >= 10 | Left value is greater or equal |
| <= | Less Than or Equal To | 10 <= 10 | Left value is smaller or equal |
1. Equal To Operator (=)
The equal sign = is used to check whether two values are equal.
Result: True
VBA Example
Since A and B both contain 10, the comparison A = B returns True.
Important: Assignment vs Comparison
The same = symbol is also used when assigning a value to a variable.
Here, VBA is assigning 10 to A.
Here, VBA is comparing A with 10.
A = 10 outside a condition normally assigns a value.
A = 10 inside a condition compares values.
2. Not Equal To Operator (<>)
The <> operator checks whether two values are different.
Result: True
Because 10 and 5 are different.
Example
This is useful when you want to execute code only when a value is different from a particular value.
3. Greater Than Operator (>)
The greater-than operator checks whether the first value is greater than the second value.
Result: True
Sales Target Example
This type of comparison is frequently used in financial reports and Excel automation.
4. Less Than Operator (<)
The less-than operator checks whether the first value is smaller than the second value.
Result: True
Example
5. Greater Than or Equal To (>=)
The >= operator returns True when the first value is either greater than or equal to the second value.
Result: True
Notice that equality is included.
Example – Passing Marks
A student with exactly 40 marks will pass because >= includes equality.
6. Less Than or Equal To (<=)
The <= operator returns True when the first value is either smaller than or equal to the second value.
Result: True
Example
Comparison Operators Return True or False
Comparison expressions can be stored in a Boolean variable.
The value of Result will be:
Another Example
The result will be:
Using Comparison Operators with If...Then
Comparison operators become especially powerful when combined with decision-making statements such as If...Then.
VBA first evaluates:
75000 > 50000
Result = True
Using Comparison Operators with If...Else
You can use comparison operators to choose between two different actions.
This is one of the most common uses of comparison operators in VBA programming.
Combining Comparison Operators
Comparison operators can be combined with logical operators such as And, Or and Not.
Example Using And
Here VBA checks two comparisons:
- Age >= 18
- Salary >= 50000
Both must be True for the complete condition to be True.
Comparing Text in VBA
Comparison operators can also be used with text values.
VBA compares the text expressions according to the applicable string comparison rules. The exact behavior of text comparison can also depend on the module's Option Compare setting.
When comparing strings, be careful about spelling, spaces and comparison settings.
Comparing Dates in VBA
Comparison operators can also be used to compare dates. This is very useful in Excel reports, attendance systems, invoice tracking and deadline management.
You can also check whether a date has already passed.
Comparing Excel Cell Values
Comparison operators are extremely useful when working directly with worksheet cells.
Example
Suppose B2 contains:
VBA evaluates:
Result = True
Practical Example – Employee Performance
Suppose an Excel worksheet contains an employee's sales performance.
| Cell | Information |
|---|---|
| A2 | Employee Name |
| B2 | Sales |
| C2 | Target |
VBA can compare Sales and Target:
This simple concept can be expanded into complete automated sales dashboards and reporting systems.
Using ElseIf with Comparison Operators
You can test multiple ranges using ElseIf.
VBA evaluates the conditions from top to bottom and executes the first condition that evaluates to True.
Comparison with Null
One important VBA concept is Null. A Null value represents the absence of a valid value in a Variant.
If one of the expressions being compared is Null, the comparison result can also be Null rather than True or False.
Do not normally test for Null using:
Value = Null
Use:
IsNull(Value)
when you need to determine whether a Variant contains Null.
Common Mistakes
1. Confusing = with <>
<> means not equal.
2. Confusing > with >=
The operator > does not include equality.
The operator >= includes equality.
10 >= 10 = True
3. Confusing < with <=
10 <= 10 = True
4. Forgetting Quotes Around Text
Text values should normally be written as string literals using quotation marks.
Practice Questions
Try to solve these questions yourself before checking the answers.
What is the result of:
20 > 10
What is the result of:
20 < 10
What is the result of:
50 >= 50
What is the result of:
50 <= 49
Write VBA code to check whether the value in B2 is greater than 1000.
Write VBA code to check whether a variable named Status is not equal to "Completed".
Write VBA code to check whether Marks are greater than or equal to 40.
Quick Quiz
A. !=
B. <>
C. <=
D. ==
A. True
B. False
C. 25
D. 10
A. True
B. False
C. Null
D. Error
A. >
B. <
C. >=
D. <>
A. IsEmpty
B. IsNull
C. IsNothing
D. NullCheck
Key Takeaways
- = checks whether two values are equal.
- <> checks whether two values are different.
- > checks whether the first value is greater.
- < checks whether the first value is smaller.
- >= means greater than or equal to.
- <= means less than or equal to.
- Comparison expressions are commonly used with If...Then.
- Comparison operators can be combined with logical operators such as And and Or.
- Numbers, text and dates can all be compared in VBA, subject to their data types and comparison rules.
- Be careful when comparing values that may contain Null.
What's Next?
Now that you understand Comparison Operators, the next important topic is Logical Operators.
Logical operators allow you to combine multiple conditions. They are especially important when creating advanced If...Then statements and decision-making systems in VBA.
Learn Logical Operators →
0 Comments