VBA – Comparison Operators

  • Post author:
  • Post category:VBA
  • Post comments:1 Comment
comparison

There are the following comparison operators supported by VBA.

Assume variable A holds 10 and variable B holds 20, then −

OperatorDescriptionExample
=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.

Example

Try the following example to understand all the Comparison operators available in 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

When you execute the above script, it will produce the following result.

Operator Line 1 : False

Operator Line 2 : True

Operator Line 3 : False

Operator Line 4 : True

Operator Line 5 : False

Operator Line 6 : True

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply