In VBA operators are as important as they are in other languages or work, in VBA we can use all the operators such as addition, multiplication subtraction division assignment or the logical operators, the method is similar to use the operators as we use in excel such as A>B is using a comparison operator.
What are Operators in VBA?
It doesn’t matter how good we are or how proficient we are at our work, if we don’t do the basic right, then everything will be in a mess. Firstly if we don’t learn the basics right then we cannot progress to the next level, be it any profession. The reason why I am pressing so much on basics because in today’s article we will show you one of the basic concepts “VBA Operators”.
VBA Operators are the heart of any calculation. Operators are the signs we use to compare one thing with another, operators are the signs we use to check whether one number is greater than another or less than another or equal to another number and not equal to as well.
I am sure you must have used these logics in your daily workplace. Below is the mathematical operator’s list we use regularly.
Above are the mathematical operators and those are common to everybody. We have comparison operators as well, below are the list of those.
List of Comparison Operators for VBA
- Equal Sign (=): This sign is used to compare whether one thing is equal to another thing. The result of this operator sign is either TRUE or FALSE. If the one thing is equal to another then we will get TRUE or else FALSE.
For example an equation 25 = 25 returns TRUE and an equation Hello = Hiii returns FALSE. - Greater Than Sign (>): This sign checks whether one number is greater than the other number. This is also a logical VBA operator where the result is either TRUE or FALSE.
For example, an equation 25 > 20 returns TRUE but 25 > 25 returns FALSE because 25 is equal to 25 not greater than that number. - Greater Than or Equal to Sign (>=): This sign works exactly the same as the above operator Greater Than but checks whether the number is equal or not.
- Less than Sign (>): This sign checks whether one number is less than the other number. This is also a logical operator in VBA where the result is either TRUE or FALSE.
For example an equation 20 < 25 returns TRUE but 25 < 25 returns FALSE because 25 is equal to 25 not greater than that number. - Less Than or Equal to Sign (>=): This sign works exactly the same as the above VBA operator Less Than but checks whether the number is equal or not.
- Not Equal to Sign (<>): This not equal to sign is the inverse operator returns inverse results. If the one thing is equal to another then it returns FALSE or else TRUE.
For example, equation 25 <> 20 returns TRUE because the number 25 is not equal to the number 20. 30 <> 30 returns FALSE because 30 is equal to 30 only.
Example Code of Operator Sign in VBA
Below are the examples of VBA comparison operators in excel.
4.6 (247 ratings)
Example #1 – Equal Sign Code
Below is the VBA Code to understand the use of Equal(=) operator.
Code:
Sub Equal_Operator() Dim Val1 As String Dim Val2 As String Val1 = 25 Val2 = 25 If Val1 = Val2 Then MsgBox "Both are same and result is TRUE" Else MsgBox "Both are not same and result is FALSE" End If End Sub
This will return the result as TRUE because variables values “Val1” & “Val2” are the same.
Example #2 – Greater Than Sign Code
Below is the VBA Code to understand the use of Greater Than (>) operator.
Code:
Sub Greater_Operator() Dim Val1 As String Dim Val2 As String Val1 = 25 Val2 = 20 If Val1 > Val2 Then MsgBox "Val1 is greater than the val2 and result is TRUE" Else MsgBox "Val1 is not greater than the val2 and result is FALSE" End If End Sub
And the result will be –
Example #3 – Greater Than or Equal to Sign Code
Below is the VBA Code to understand the use of Greater Than or Equal to(>=) operator.
Code:
Sub Greater_Than_Equal_Operator() Dim Val1 As String Dim Val2 As String Val1 = 25 Val2 = 20 If Val1 >= Val2 Then MsgBox "Val1 is greater than the val2 and result is TRUE" Else MsgBox "Val1 is not greater than the val2 and result is FALSE" End If End Sub
Now we will just change the val2 amount to 25 and then run the code.
Both the results return TRUE because we have applied >= sign.
Example #4 – Less Than Sign Code
Below is the VBA Code to understand the use of Less Than (<) operator.
Code:
Sub Less_Operator() Dim Val1 As String Dim Val2 As String Val1 = 25 Val2 = 20 If Val1 < Val2 Then MsgBox "Val1 is less than the val2 and result is TRUE" Else MsgBox "Val1 is not less than the val2 and result is FALSE" End If End Sub
This returns FALSE because 25 is not less than 20.
Example #5 – Not Equal to Sign Code
Below is the VBA Code to understand the use of Not Equal (<>) operator.
Code:
Sub NotEqual_Operator() Dim Val1 As String Dim Val2 As String Val1 = 25 Val2 = 20 If Val1 <> Val2 Then MsgBox "Val1 is not equal to val2 and result is TRUE" Else MsgBox "Val1 is equal to val2 and result is FALSE" End If End Sub
You will get the following output.
Recommended Articles
This has been a guide to VBA Operators. Here we provide the list of VBA comparison operators along with practical examples and downloadable template codes. You can learn more about VBA from the following articles –
- VBA Switch Case Statement
- Activate Sheet in VBA | Using its Index Number
- What does FileDialog Option do in VBA?
- InStr in VBA
- VBA GetOpenFilename
- List of Logical Operators in Excel
- Step by Step Tutorial in VBA
- Assign Data Type in VBA
- Use IF & AND Function in Excel
- 3 Courses
- 12 Hands-on Projects
- 43+ Hours
- Full Lifetime Access
- Certificate of Completion