VBA Comparison Operator

VBA Comparison Operator. Comparison operators can be used to compare numbers or strings and perform evaluations. Expressions using comparison operators do not return a number value, as do arithmetic expressions. Comparison expressions return either 1, which is true, or 0, which is false. Some comparison operators include equal to, less than, greater than, and not equal to.

In VBA, compression operators include less than (<), greater than (>), equal to (=), less than equal to (<=), greater than equal to (>=), and not equal to (<>).

The following examples show the comparison operators defined for VBA

1. Equal Sign (=)

This operator sign can be used to find out whether one thing is equal to another. The result will be TRUE or FALSE. If one thing is greater than another, then TRUE is produced, and False is produced.

Example

Sub EqualOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 20

If iValue_1 = iValue_2 Then

MsgBox "If both are the same and result will be TRUE"

Else

MsgBox "If both are not same and result will be FALSE"

End If

End Sub

2. Not Equal to (<>)

Not equal to operator is used to check if two values are not equal. If one thing is equal to another. If one thing is equal to another, it returns FALSE or TRUE.

Example

Sub NotEqualOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 20

If iValue_1 <> iValue_2 Then

MsgBox "If both values are not same then result will be TRUE"

Else

MsgBox "If both values are same then result will be FALSE"

End If

End Sub
See More: VBA Arithmetic Operators

3. Greater Than (>)

This symbol determines whether one number is greater than the other, and is also a logical VBA operator with a return of either TRUE or FALSE.

Example

Sub GreaterThanOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 20

If iValue_1 > iValue_2 Then

MsgBox "If iValue_1 is greater than to iValue_2 then result will be TRUE"

Else

MsgBox "If iValue_1 is not greater than to iValue_2 then result will be FALSE"

End If

End Sub

4. Greater Than Equal to (> =)

The greater than equal to operator is used to check if the first value is greater than or equal to the second value and returns True or False.

Example

Sub GreaterThanEqualOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 20

If iValue_1 >= iValue_2 Then

MsgBox "If iValue_1 is greater than or equal to iValue_2 then result will be TRUE"

Else

MsgBox "If iValue_1 is not greater than or not equal to iValue_2 then result will be FALSE"

End If

End Sub

5. Less Than ( < )

This sign checks whether one number is less than the other number. It is also a logical operator in VBA, where the result is either TRUE or FALSE.

Example

Sub LessThanOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 30

If iValue_1 < iValue_2 Then

MsgBox "If iValue_1 is Less than to iValue_2 then result will be TRUE"

Else

MsgBox "If iValue_1 is not Less than to iValue_2 then result will be FALSE"

End If

End Sub

6. Less Than Equal to ( < = )

Less than equal operator is used to checking if the value that comes first is less than or equal to the second value and returns True or False.

Example

Sub LessThanEqualOperator()

Dim iValue_1 As Integer

Dim iValue_2 As Integer

iValue_1 = 30

iValue_2 = 20

If iValue_1 <= iValue_2 Then

MsgBox "If iValue_1 is Less than or Equal to iValue_2 then result will be TRUE"

Else

MsgBox "If iValue_1 is not Less than or not Equal to iValue_2 then result will be FALSE"

End If

End Sub

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top