contents.gifindex.gifprev1.gifnext1.gif

Comparison Operators

Purpose

Used to compare two expressions.

Syntax

result = expr1 operator expr2

Notes

Comparison operators, also known as relational operators, compare two expressions. The following table contains a list of the comparison operators and the conditions that determine whether result is True, False, or Null.

Operator
Meaning
True if
False if
Null if


<
Less than
expr1 < expr2
expr1 >= expr2
expr1 or expr2 = Null
<=
Less than or equal to
expr1 <= expr2
expr1 > expr2
expr1 or expr2 = Null
>
Greater than
expr1 > expr2
expr1 <= expr2
expr1 or expr2 = Null
>=
Greater than or equal to
expr1 >= expr2
expr1 < expr2
expr1 or expr2 = Null
=
Equal to
expr1 = expr2
expr1 <> expr2
expr1 or expr2 = Null
<>
Not equal to
expr1 <> expr2
expr1 = expr2
expr1 or expr2 = Null


When comparing two expressions, you may not be able to easily determine whether the expressions are being compared as numbers or as strings. The following table shows how the expressions are compared or what results when either expression is not a Variant.

If
Then


Both expressions are numeric data types (Integer, Long, Single, Double, or Currency)
Perform a numeric comparison.
Both expressions are String
Perform a string comparison.
One expression is a numeric data type and the other is a Variant of VarType 2-7 (a numeric data type) or VarType 8 (String) that can be converted to a number
Perform a numeric comparison.
One expression is a numeric data type and the other is a Variant of VarType 8 (String) that can't be converted to a number
The numeric expression is less than the String expression.
One expression is a String and the other is a Variant of VarType 8 (String)
Perform a string comparison.
One expression is Empty and the other is a numeric data type
Perform a numeric comparison.
One expression is Empty and the other is a String
Perform a string comparison.
One expression is a numeric data type and the other is a Variant of VarType 8 (String)
A Type mismatch error occurs.


If expr1 and expr2 are both Variant expressions, their VarType determines how they are compared. The following table shows how the expressions are compared or what results from the comparison, depending on the VarType of the Variant.

If
Then


Both Variant expressions are of VarType 2-7 (numeric data types)
Perform a numeric comparison.
Both Variant expressions are of VarType 8 (String)
Perform a string comparison.
One Variant expression is of VarType 2-7 (a numeric data type) and the other is of VarType 8 (String)
The numeric expression is less than the String expression.
One Variant expression is Empty and the other is of VarType 2-7 (a numeric data type)
Perform a numeric comparison.
One Variant expression is Empty and the other is of VarType 8 (String)
Perform a string comparison.
Both Variant expressions are Empty
The expressions are equal.


Note If a Currency is compared with a Single or Double, the Single or Double is converted to a Currency. This causes any fractional part of the Single or Double value less than 0.0001 to be lost and may cause two values to compare as equal when they are not. Such a conversion can also cause an Overflow error if the magnitude of the Single or Double is too great.

Example

This example shows a typical use of a comparison operator to evaluate the relationship between variables A and B. An appropriate message prints depending on whether the expression A <= B is True (-1) or False (0). The other comparison operators can be used in a similar way.

If A <= B Then

Debug.Print "A is less than or equal to B."

Else

Debug.Print "A is greater than B."

End If