Table of contents
TOC
Collapse the table of content
Expand the table of content

Comparison Operators

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

Used to compare expressions. Syntaxresult=expression1comparisonoperatorexpression2result=object1Isobject2result=stringLikepatternComparison operators have these parts:

PartDescription
resultRequired; any numeric variable.
expressionRequired; any expression.
comparisonoperatorRequired; any comparison operator.
objectRequired; any object name.
stringRequired; any string expression.
patternRequired; any string expression or range of characters.

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

OperatorTrue ifFalse ifNull if
< ( Less than)expression1 < expression2expression1 >= expression2expression1 or expression2 = Null
<= ( Less than or equal to)expression1 <= expression2expression1 > expression2expression1 or expression2 = Null
> ( Greater than)expression1 > expression2expression1 <= expression2expression1 or expression2 = Null
>= ( Greater than or equal to)expression1 >= expression2expression1 < expression2expression1 or expression2 = Null
= ( Equal to)expression1 = expression2expression1 <> expression2expression1 or expression2 = Null
<> ( Not equal to)expression1 <> expression2expression1 = expression2expression1 or expression2 = Null

Note The Is and Like operators have specific comparison functionality that differs from the operators in the table.

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:

IfThen
Both expressions are numeric data types (Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal)Perform a numeric comparison.
Both expressions are StringPerform a string comparison.
One expression is a numeric data type and the other is a Variant that is, or can be, a numberPerform a numeric comparison.
One expression is a numeric data type and the other is a string Variant that can't be converted to a numberA Type Mismatch error occurs.
One expression is a String and the other is any Variant except a NullPerform a string comparison.
One expression is Empty and the other is a numeric data typePerform a numeric comparison, using 0 as the Empty expression.
One expression is Empty and the other is a StringPerform a string comparison, using a zero-length string ("") as the Empty expression.

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:

IfThen
Both Variant expressions are numericPerform a numeric comparison.
Both Variant expressions are stringsPerform a string comparison.
One Variant expression is numeric and the other is a stringThe numeric expression is less than the string expression.
One Variant expression is Empty and the other is numericPerform a numeric comparison, using 0 as the Empty expression.
One Variant expression is Empty and the other is a stringPerform a string comparison, using a zero-length string ("") as the Empty expression.
Both Variant expressions are EmptyThe expressions are equal.

When a Single is compared to a Double, the Double is rounded to the precision of the Single. If a Currency is compared with a Single or Double, the Single or Double is converted to a Currency. Similarly, when a Decimal is compared with a Single or Double, the Single or Double is converted to a Decimal. For Currency, any fractional value less than .0001 may be lost; for Decimal, any fractional value less than 1E-28 may be lost, or an overflow error can occur. Such fractional value loss may cause two values to compare as equal when they are not.

Example

This example shows various uses of comparison operators, which you use to compare expressions.

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

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

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

Var1 = 0: Var2 = Empty
MyResult = (Var1 = Var2)    ' Returns True.
© 2018 Microsoft