Comparison Operators

Used to compare expressions.

Syntax

result = expression1 comparisonoperator expression2

Comparison operators have these parts:

Part Description
result Required; any numeric variable.
expression1 Required; any expression.
expression2 Required; any expression.
comparisonoperator Required; any comparison operator.

Remarks

The following table contains a list of the comparison operators and the conditions that determine whether result is True or False:

Operator True if False if
< (Less than) expression1 < expression2 expression1 >= expression2
<= (Less than or equal to) expression1 <= expression2 expression1 > expression2
> (Greater than) expression1 > expression2 expression1 <= expression2
>= (Greater than or equal to) expression1 >= expression2 expression1 < expression2
= (Equal to) expression1 = expression2 expression1 <> expression2
<> (Not equal to) expression1 <> expression2 expression1 = expression2

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 the result when either expression is not a Variant:

If Then
Both expressions are numeric data types (Byte, Boolean, Integer, Long, Single, Double, or Date) 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 that is, or can be, a number Perform a numeric comparison.
One expression is a numeric data type and the other is a string Variant that can't be converted to a number A Type Mismatch error occurs.

If expression1 and expression2 are both Variant expressions, their underlying type determines how they are compared. The following table shows how the expressions are compared or the result from the comparison, depending on the underlying type of the Variant:

If Then
Both Variant expressions are numeric Perform a numeric comparison.
Both Variant expressions are strings Perform a string comparison.
One Variant expression is numeric and the other is a string The numeric expression is less than the string expression.

Example

Dim MyResult, Var1, Var2
MyResult = (45 < 35)   ' Returns False.
trace MyResult
MyResult = (45 = 45)   ' Returns True.
trace MyResult
MyResult = (4 <> 3)   ' Returns True.
trace MyResult
MyResult = ("5" > "4")   ' Returns True.
trace MyResult

Var1 = "5": Var2 = 4   ' Initialize variables.
MyResult = (Var1 > Var2)   ' Returns True.
trace MyResult

Var1 = 5: Var2 = Empty
MyResult = (Var1 > Var2)   ' Returns True.
trace MyResult

Var1 = 0: Var2 = Empty
MyResult = (Var1 = Var2)   ' Returns True.
trace MyResult

 

See Also

Operators