HKCareers Free Resource
VBA ─ Operators
The built-in VBA operators consist of mathematical operators, string operators, comparison
operators and logical operators. The different types of Operators are discussed individually
below. VBA supports following types of operators:
❖ Arithmetic Operators
❖ Comparison Operators
❖ Logical (or Relational) Operators
❖ Concatenation Operators
The Arithmetic Operators
In the expression: 9 minus 5 is equal to 4; 9 and 5 are called operands and - is called operator.
Following arithmetic operators are supported by VBA:
Operato
r Description
+ Adds the two operands
- Subtracts the second operand from the first
* Multiplies both the operands
/ Divides the numerator by the denominator
% Modulus operator and the remainder after an integer division
^ Exponentiation operator
Arithmetic Operators ─ Example
You can add a button and try the example below to understand all the arithmetic operators
available in VBA.
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
Private Sub Constant_demo_Click()
Dim a As Integer
a=5
Dim b As Integer
b = 10
Dim c As Double
c=a+b
MsgBox ("Addition Result is " & c)
c=a-b
MsgBox ("Subtraction Result is " & c)
c=a*b
MsgBox ("Multiplication Result is " & c)
c=b/a
MsgBox ("Division Result is " & c)
c = b Mod a
MsgBox ("Modulus Result is " & c)
c=b^a
MsgBox ("Exponentiation Result is " & c)
End Sub
Once executed, the above script will display the following results
➔ Addition Result is 15
➔ Subtraction Result is -5
➔ Multiplication Result is 50
➔ Division Result is 2
➔ Modulus Result is 0
➔ Exponentiation Result is 100000
The Comparison Operators
Comparison operators compare two numbers or strings and return a logical (True or False)
result. The main Excel VBA comparison operators are listed in the table below:
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
Assume variable A holds 10 and variable B holds 20, then -
Operato Exampl
r Description e
Checks if the value of the two operands are equal or not. If yes, then the
condition ( A == B)
==
is true. False.
Checks if the value of the two operands are equal or not. If the values are not A <> B) is
<>
equal, then the condition is true. True.
(
Checks if the value of the left operand is greater than the value of the right A> B) is
>
operand. If yes, then the condition is true. False.
Checks if the value of the left operand is less than the value of the right (
operand. A< B) is
<
If yes, then the condition is true. True.
Checks if the value of the left operand is greater than or equal to the value of (
the A >= B) is
>=
right operand. If yes, then the condition is true. False.
(
Checks if the value of the left operand is less than or equal to the value of the A <= B) is
<=
right operand. If yes, then the condition is true. True.
Comparison Operators ─ Example
These examples discussed here will be helpful towards developing your understanding of
comparison operators:-
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
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
Once executed, this script will generate the following results:-
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
Operator Line 1 : False
Operator Line 2 : True
Operator Line 3 : False
Operator Line 4 : True
Operator Line 5 : False
Operator Line 6 : True
The Logical Operators
Logical operators compare Boolean expressions and return Boolean results. Hence these also
return a logical (True or False) result. The main Excel VBA logical operators are listed in the
table below:
Assume variable A holds 10 and variable B holds 0, then:-
Operator Description Example
AND If both conditions evaluate to True then the a<>0 AND b<>0 is False
expression is True
OR Performs logical disjunction or inclusion a<>0 OR b<>0 is True
on two Boolean expressions. If either
expression evaluates to True, or both
evaluate to True, then Or returns True. If
neither expression evaluates to True, Or
returns False.
NOT Performs logical negation on a Boolean NOT(a<>0 OR b<>0) is False
expression. It yields the logical opposite of
its operand. If the expression evaluates to
True, then Not returns False; if the
expression evaluates to False, then Not
returns True
XOR Performs logical exclusion on two Boolean (a<>0 XOR b<>0) is False
expressions. If exactly one expression
evaluates to True, but not both, Xor returns
True. If both expressions evaluate to True
or both evaluate to False, Xor returns
False.
Logical Operators ─ Example
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
You can try some of the Logical operators available in VBA by creating a button and adding
the following function.
Private Sub Constant_demo_Click()
Dim a As Integer
a = 10
Dim b As Integer
b=0
If a <> 0 And b <> 0 Then
MsgBox ("AND Operator Result is : True")
Else
MsgBox ("AND Operator Result is : False")
End If
If a <> 0 Or b <> 0 Then
MsgBox ("OR Operator Result is : True")
Else
MsgBox ("OR Operator Result is : False")
End If
If Not (a <> 0 Or b <> 0) Then
MsgBox ("NOT Operator Result is : True")
Else
MsgBox ("NOT Operator Result is : False")
End If
If (a <> 0 Xor b <> 0) Then
MsgBox ("XOR Operator Result is : True")
Else
MsgBox ("XOR Operator Result is : False")
End If
End Sub
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching