Vba-comparison-operators
提供:Dev Guides
VBA-比較演算子
VBAでサポートされている比較演算子は次のとおりです。
変数Aが10を保持し、変数Bが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
= | Checks if the value of the two operands are equal or not. If yes, then the condition is true. | (A = B) is False. |
<> | Checks if the value of the two operands are equal or not. If the values are not equal, then the condition is true. | (A <> B) is True. |
> | Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true. | (A > B) is False. |
< | Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true. | (A < B) is True. |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true. | (A >= B) is False. |
⇐ | Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true. | (A ⇐ B) is 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