We are in the process of updating the site, so some pages still have the older design.
VBA comparison operators are used to compare values in VBA and Macros for Excel.
|
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. |
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.
If A is less than B.
If A < B Then MsgBox "Hi!" Else MsgBox "Bye!"

If A is greater than B.
If A > B Then MsgBox "Hi!" Else MsgBox "Bye!"

If A is less than or equal to B.
If A <= B Then MsgBox "Hi!" Else MsgBox "Bye!"

If A is greater than or equal to B.
If A >= B Then MsgBox "Hi!" Else MsgBox "Bye!"

If A is equal to B.
If A = B Then MsgBox "Hi!" Else MsgBox "Bye!"

If A is not equal to B.
If A <> B Then MsgBox "Hi!" Else MsgBox "Bye!"

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.
Follow along with the tutorial by downloading the files used in it. (Completely Free)
Phone number is optional - we use it to send course discounts and weekly updates via SMS.