位置:首页 » 文章/教程分享 » VBA比较运算符

  下表列出了所有VBA支持的比较操作符。假设变量A=10和变量B=20,则:

操作符 描述 例子
== 检查两个操作数的值是否相等,如果是的话那么条件为真。 (A == B) 的值为 False.
<> 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 (A <> B)  的值为True.
> 检查,左操作数的值是否大于右操作数的值,如果是的话那么条件为真。 (A > B)  的值为 False.
< 检查,左操作数的值是否小于右操作数的值,如果是的话那么条件为真。 (A < B)  的值为 True.
>= 检查左边的操作数的值是否大于或等于右操作数的值,如果是的话那么条件为真。 (A >= B)  的值为 False.
<= 检查,左边的操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 (A <= B)  的值为 True.

例子

试试下面的例子就明白了所有VBA提供的比较操作:

Private Sub Constant_demo_Click() Dim a: a = 10 Dim b: b = 20 Dim c If a = b Then MsgBox ("Operator Line 1 : True") Else MsgBox ("Operator Line 1 : False") End If If a<>b Then MsgBox ("Operator Line 2 : True") Else MsgBox ("Operator Line 2 : False") End If If a>b Then MsgBox ("Operator Line 3 : True") Else MsgBox ("Operator Line 3 : False") End If If a<b Then MsgBox ("Operator Line 4 : True") Else MsgBox ("Operator Line 4 : False") End If If a>=b Then MsgBox ("Operator Line 5 : True") Else MsgBox ("Operator Line 5 : False") End If If a<=b Then MsgBox ("Operator Line 6 : True") Else MsgBox ("Operator Line 6 : False") End If End Sub

当执行上面的脚本时,会产生以下结果:

Operator Line 1 : False

Operator Line 2 : True

Operator Line 3 : False

Operator Line 4 : True

Operator Line 5 : False

Operator Line 6 : True