比较运算符Comparison operators

用于比较表达式Used to compare expressions.

语法Syntax

result = _表达式_1 comparisonoperator _表达式_2result = expression1 comparisonoperator expression2
结果 = object1 object2result = object1 Is object2
结果 = 字符串,模式result = string Like pattern

比较运算符包含以下部分:Comparison operators have these parts:

PartPart 说明Description
resultresult 必需,任何数值变量Required; any numeric variable.
expressionexpression 必需;任何表达式。Required; any expression.
comparisonoperatorcomparisonoperator 必需;任何比较运算符。Required; any comparison operator.
objectobject 必需;任何对象名称。Required; any object name.
stringstring 必需;任何字符串表达式Required; any string expression.
patternpattern 必需;任何字符串表达式或字符范围。Required; any string expression or range of characters.

备注Remarks

下表包含比较运算符和条件的列表, 这些运算符决定了_result_是还是NullThe following table contains a list of the comparison operators and the conditions that determine whether result is True, False, or Null.

OperatorOperator 满足以下条件时为 TrueTrue if 满足以下条件时为 FalseFalse if 满足以下条件时为 NullNull if
<(小于)< (Less than) 表达式 < 2_表达式_2expression1 < expression2 表达式 >= 2_表达式_2expression1 >= expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null
<=(小于或等于)<= (Less than or equal to) 表达式 <= 2_表达式_2expression1 <= expression2 表达式 > 2_表达式_2expression1 > expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null
>(大于)> (Greater than) 表达式 > 2_表达式_2expression1 > expression2 表达式 <= 2_表达式_2expression1 <= expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null
>=(大于或等于)>= (Greater than or equal to) 表达式 >= 2_表达式_2expression1 >= expression2 表达式 < 2_表达式_2expression1 < expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null
=(等于)= (Equal to) 表达式 = 2_表达式_2expression1 = expression2 表达式 <> 2_表达式_2expression1 <> expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null
<>(不等于)<> (Not equal to) 表达式 <> 2_表达式_2expression1 <> expression2 表达式 = 2_表达式_2expression1 = expression2 表达式_1 或_表达式 = 2Nullexpression1 or expression2 = Null

备注

[!注释] IsLike 运算符具有与表中的运算符不同的特定的比较功能。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. 下表显示了表达式的比较方式或者当任一表达式不是Variant时的结果。The following table shows how the expressions are compared or the result when either expression is not a Variant.

IfIf ThenThen
两个表达式都为数值数据类型ByteBooleanIntegerLongSingleDoubleDateCurrencyDecimalBoth expressions are numeric data types (Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal) 执行数值比较。Perform a numeric comparison.
两个表达式都为 StringBoth expressions are String 执行字符串比较Perform a string comparison.
一个表达式为数值数据类型,另一个表达式为 Variant(即为数字或可为数字)One expression is a numeric data type and the other is a Variant that is, or can be, a number 执行数值比较。Perform a numeric comparison.
一个表达式为数值数据类型,另一个表达式为无法转换为数字的字符串 VariantOne expression is a numeric data type and the other is a string Variant that can't be converted to a number Type Mismatch发生错误。A Type Mismatch error occurs.
一个表达式为 String,另一个表达式为除 Null 之外的任何 VariantOne expression is a String and the other is any Variant except a Null 执行字符串比较。Perform a string comparison.
一个表达式为 Empty,另一个表达式为数值数据类型One expression is Empty and the other is a numeric data type 执行数值比较,并将 0 用作 Empty 表达式。Perform a numeric comparison, using 0 as the Empty expression.
一个表达式为 Empty,另一个表达式为 StringOne expression is Empty and the other is a String 执行字符串比较,并将零长度字符串 ("") 用作 Empty 表达式。Perform a string comparison, using a zero-length string ("") as the Empty expression.

如果 expression1expression2 都为 Variant 表达式,则这两个表达式的基础类型将确定其比较方式。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.

IfIf ThenThen
两个 Variant 表达式都为数值表达式Both Variant expressions are numeric 执行数值比较。Perform a numeric comparison.
两个 Variant 表达式都为字符串表达式Both Variant expressions are strings 执行字符串比较。Perform a string comparison.
一个 Variant 表达式为数值表达式,另一个表达式为字符串表达式One Variant expression is numeric and the other is a string 数值表达式小于字符串表达式。The numeric expression is less than the string expression.
一个 Variant 表达式为 Empty 表达式,另一个表达式为数值表达式One Variant expression is Empty and the other is numeric 执行数值比较,并将 0 用作 Empty 表达式。Perform a numeric comparison, using 0 as the Empty expression.
一个 Variant 表达式为 Empty 表达式,另一个表达式为字符串表达式One Variant expression is Empty and the other is a string 执行字符串比较,并将零长度字符串 ("") 用作 Empty 表达式。Perform a string comparison, using a zero-length string ("") as the Empty expression.
两个 Variant 表达式都为 Empty 表达式Both Variant expressions are Empty 两个表达式相等。The expressions are equal.

在将 SingleDouble 进行比较时,会将 Double 舍入到 Single 的精度。When a Single is compared to a Double, the Double is rounded to the precision of the Single. 如果将 CurrencySingleDouble 进行比较,则 SingleDouble 将转换为 CurrencyIf a Currency is compared with a Single or Double, the Single or Double is converted to a Currency.

同样,在将 DecimalSingleDouble 进行比较时,则 SingleDouble 将转换为 DecimalSimilarly, when a Decimal is compared with a Single or Double, the Single or Double is converted to a Decimal. 对于 Currency,可能会丢失任何小于 .0001 的小数值;对于 Decimal,可能会丢失任何小于 1E-28 的小数值,或发生溢出错误。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.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.