Comparison Operator
Comparison operators are used to compare values and return a Boolean result (True or False). These operators are essential for making decisions and controlling the flow of your VBA code.
Syntax:
Purpose: Checks if two values are equal.
expression1 = expression2
Sub equalto_demo()
If 5 = 5 Then
MsgBox "The numbers are equal"
End If
End Sub
Example
Syntax:
Purpose: Checks if two values are not equal.
expression1 <> expression2
Sub notequalto_demo()
If "Hello" <> "World" Then
MsgBox "The strings are different"
End If
End Sub
Example
Syntax:
Purpose: Checks if the first value is less than the second.
expression1 < expression2
Sub lessthan_demo()
If 10 < 20 Then
MsgBox "10 is less than 20"
End If
End Sub
Example
Syntax:
Purpose: Checks if the first value is greater than the second.
expression1 > expression2
Sub lessthan_demo()
If 20 > 10 Then
MsgBox "20 is greater than 10"
End If
End Sub
Example
Syntax:
Purpose: Checks if the first value is less than or equal to the second.
expression1 <= expression2
Sub lessthan_demo()
If 10 <= 10 Then
MsgBox "10 is less than or equal to 10"
End If
End Sub
Example
Syntax:
Purpose: Checks if the first value is greater than or equal to the second.
expression1 >= expression2
Sub lessthan_demo()
If 20 >= 10 Then
MsgBox "20 is greater than or equal to 10"
End If
End Sub
Example
These comparison operators are commonly used in conditional statements like If...Then...Else
and Select Case
to control the flow of your VBA code based on specific conditions.