VBA Comparison Operators

Add to Favorites
Author: don

VBA comparison operators are used to compare values in VBA and Macros for Excel.

List of VBA Comparison Operators

Comparison Operator

Meaning

<

A < B - Checks if A is LESS THAN B.

>

A > B - Checks if A is GREATER THAN B.

<=

A <= B - Checks if A is less than or equal to B.

>=

A >= B - Checks if A is greater than or equal to B.

=

A = B - Checks if A is equal to B. Note that a single equal sign is also used to assign a value to a variable when it is not being used in the context of a comparison.

<>

A <> B - Checks if A is NOT equal to B.

Comparison Operator Examples

Each example uses an IF statement to compare the values A and B and it will then output a message depending on the result of the comparison.

The part to pay attention to comes between the If and Then section of the code.

A < B

If A is less than B.

If A < B Then MsgBox "Hi!" Else MsgBox "Bye!"

8808c880e64199eadd36219599316b0f.jpg

A > B

If A is greater than B.

If A > B Then MsgBox "Hi!" Else MsgBox "Bye!"

1a63d38cc88c2cd3823ca94f8ad635b4.jpg

A <= B

If A is less than or equal to B.

If A <= B Then MsgBox "Hi!" Else MsgBox "Bye!"

5a4ddd413623f0c08035e5e374f5da26.jpg

A >= B

If A is greater than or equal to B.

If A >= B Then MsgBox "Hi!" Else MsgBox "Bye!"

7613d066a1a56dfc90b05e36467d4dd3.jpg

A = B

If A is equal to B.

If A = B Then MsgBox "Hi!" Else MsgBox "Bye!"

15b418028c7329931b4b6d64794b0819.jpg

A <> B

If A is not equal to B.

If A <> B Then MsgBox "Hi!" Else MsgBox "Bye!"

8812d43ce218ca6e160dda5ce35dd7fe.jpg

Notes

Comparison operators are pretty straightforward but it does take a little time to get used to using them.

Bookmark this guide for future reference.

Download the file attached to this tutorial for the VBA code in Excel.

Question? Ask it in our Excel Forum


Downloadable Files: Excel File

Similar Content on TeachExcel
Logical Comparison Operators in Excel - How to Compare Things
Tutorial: Logical comparison operators allow you to compare values in Excel to see if they are equal...
VBA IF Statement in Excel Macros
Tutorial: IF statements allow you to make decisions in your Macros and VBA in Excel. An IF statement...
Logical Operators in Excel VBA Macros
Tutorial: Logical operators in VBA allow you to make decisions when certain conditions are met. They...
SUMIF - Sum Values Based on Criteria in Excel
Tutorial: The SUMIF function allows you to sum values based on a single criteria. This function wor...
Using Parentheses in Excel
Tutorial: How to use parentheses in Excel in order to create more powerful formulas and functions. S...
SUMIFS - Sum Values Based on Multiple Criteria in Excel
Tutorial: The SUMIFS function allows you to sum values that meet multiple criteria across multiple ...