Excel VBA 逻辑运算符
逻辑运算符用于对一组值或变量执行逻辑运算和哮喘运算。该表描述了 Excel 支持的所有不同类型的逻辑运算符: AND (LOGICAL AND) If both the conditions are True, then the Expression is true. Example: Assume variable A holds 10 and variable B holds 0, then a<>0 AND b<>0 is False OR ( Logical OR Operator) If any of the two conditions are True, then the condition is true. Example: Assume variable A holds 10 and variable B holds 0, then a<>0 OR b<>0 is true. NOT ( Logical NOT Operator) Reverse the result. If a condition is true, then the Logical NOT operator will make false. Example: Assume variable A holds 10 and variable B holds 0, then NOT(a<>0 OR b<>0) is false. XOR ( Logical XOR Operator) It is the combination of NOT and OR Operator. If one, and only one, of the expressions, evaluate to be True, the result is True. Example: Assume variable A holds 10 and variable B holds 0, then (a<>0 XOR b<>0) is trueOperator Description
1. AND(逻辑与)
如果两个条件都为真,则表达式为真。
例子:
假设变量 A 为 20,变量 B 为 0,则 a<>0 AND b<>0 为 False
程序:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a <> 0 And b <> 0 Then
MsgBox ("AND LOGICAL Operator Result is : True")
Else
MsgBox ("AND LOGICAL Operator Result is : False")
End If
End Sub
输出:
AND LOGICAL Operator Result is : False
2. OR(逻辑或运算符)
如果两个条件中的任何一个为真,则该条件为真。
例子:
假设变量 A 为 20,变量 B 为 0,则 a<>0 OR b<>0 为真。
程序:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a <> 0 Or b <> 0 Then
MsgBox ("OR LOGICAL Operator Result is : True")
Else
MsgBox ("OR LOGICAL Operator Result is : False")
End If
End Sub
输出:
OR LOGICAL Operator Result is : True
3. NOT(逻辑非运算符)
反转结果。如果条件为真,则逻辑非运算符将为假。
例子:
假设变量 A 为 20,变量 B 为 0,则 NOT(a<>0 OR b<>0) 为假。
程序:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a <> 0 Not b <> 0 Then
MsgBox ("NOT LOGICAL Operator Result is : True")
Else
MsgBox ("NOT LOGICAL Operator Result is : False")
End If
End Sub
输出:
NOT LOGICAL Operator Result is : False
4. XOR(逻辑异或运算符)
它是 NOT 和 OR 运算符的组合。如果一个且只有一个表达式的计算结果为 True,则结果为 True。
例子:
假设变量 A 为 20,变量 B 为 0,则 (a<>0 XOR b<>0) 为真。
程序:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a <> 0 Xor b <> 0 Then
MsgBox ("XOR LOGICAL Operator Result is : True")
Else
MsgBox ("XOR LOGICAL Operator Result is : False")
End If
End Sub
输出:
XOR LOGICAL Operator Result is : True
下面包括一个显示所有运算符的示例程序以及输出:
程序:
Private Sub Demo_Loop()
Dim a As Integer //Declaring variable
a = 20
Dim b As Integer //Declaring variable
b = 0
If a <> 0 And b <> 0 Then
MsgBox ("AND LOGICAL Operator Result is : True")
Else
MsgBox ("AND LOGICAL Operator Result is : False")
End If
If a <> 0 Or b <> 0 Then
MsgBox ("OR LOGICAL Operator Result is : True")
Else
MsgBox ("OR LOGICAL Operator Result is : False")
End If
If Not (a <> 0 Or b <> 0) Then
MsgBox ("NOT LOGICAL Operator Result is : True")
Else
MsgBox ("NOT LOGICAL Operator Result is : False")
End If
If (a <> 0 Xor b <> 0) Then
MsgBox ("XOR LOGICAL Operator Result is : True")
Else
MsgBox ("XOR LOGICAL Operator Result is : False")
End If
End Sub
输出:
AND LOGICAL Operator Result is : False
OR LOGICAL Operator Result is : True
NOT LOGICAL Operator Result is : False
XOR LOGICAL Operator Result is : True