目录

Show 例子

VBA支持以下比较运算符。

假设变量A保持10,变量B保持20,则 -

操作者 描述
= 检查两个操作数的值是否相等。 如果是,则条件为真。 (A = B)是假的。
<> 检查两个操作数的值是否相等。 如果值不相等,则条件为真。 (A <> B)是真的。
> 检查左操作数的值是否大于右操作数的值。 如果是,则条件为真。 (A> B)是假的。
< 检查左操作数的值是否小于右操作数的值。 如果是,则条件为真。 (A < B) 为真
>= 检查左操作数的值是否大于或等于右操作数的值。 如果是,则条件为真。 (A> = B)为假。
<= 检查左操作数的值是否小于或等于右操作数的值。 如果是,则条件为真。 (A <= B)为真。

例子 (Example)

请尝试以下示例以了解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
↑回到顶部↑
WIKI教程 @2018