Vba-logical-operators

提供:Dev Guides
移動先:案内検索

VBA-論理演算子

次の論理演算子はVBAでサポートされています。

変数Aが10を保持し、変数Bが0を保持すると仮定します-

Operator Description Example
AND Called Logical AND operator. If both the conditions are True, then the Expression is true. a<>0 AND b<>0 is False.
OR Called Logical OR Operator. If any of the two conditions are True, then the condition is true. a<>0 OR b<>0 is true.
NOT Called Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false. NOT(a<>0 OR b<>0) is false.
XOR Called Logical Exclusion. It is the combination of NOT and OR Operator. If one, and only one, of the expressions evaluates to be True, the result is True. (a<>0 XOR b<>0) is true.

次の例を試して、ボタンを作成し、次の関数を追加して、VBAで使用可能なすべての論理演算子を理解してください。

Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10
   Dim b As Integer
   b = 0

   If a <> 0 And b <> 0 Then
      MsgBox ("AND Operator Result is : True")
   Else
      MsgBox ("AND Operator Result is : False")
   End If

   If a <> 0 Or b <> 0 Then
      MsgBox ("OR Operator Result is : True")
   Else
      MsgBox ("OR Operator Result is : False")
   End If

   If Not (a <> 0 Or b <> 0) Then
      MsgBox ("NOT Operator Result is : True")
   Else
      MsgBox ("NOT Operator Result is : False")
   End If

   If (a <> 0 Xor b <> 0) Then
      MsgBox ("XOR Operator Result is : True")
   Else
      MsgBox ("XOR Operator Result is : False")
   End If
End Sub

lとして保存してInternet Explorerで実行すると、上記のスクリプトは次の結果を生成します。

AND Operator Result is : False

OR Operator Result is : True

NOT Operator Result is : False

XOR Operator Result is : True