Data Types
Data Types
❏ Explicitly: Below is an example of variable declared Explicitly. You can use "Dim"
keyword in syntax
❏ Dim Num As Integer
❏ Dim password As String
Data Types
Since the computer cannot differentiate between the numbers (1,2,3..) and strings (a,b,c,..),
Data Types are used to make this differentiation. There are many VBA data types, which can
be segregated into two main categories:
❖ Numeric Data Types
❖ Non-numeric Data Types
This table displays the numeric data types and the allowed range of values.
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
In VBA, if the data type is not specified, it will automatically declare the variable as a
Variant.
Example
Let us create a button and name it as 'Variables_demo' to demonstrate the use of variables.
*Source: Google
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
MsgBox "Password is" & password & Chr(10) & "Value of num is" & num &
Chr(10) & "Value of Birthday is" & BirthDay
End Sub
Output
Once the script is executed, the following output shall be displayed:-
Password is Admin#1
Value of num is 1234
Value of Birthday is 12:02:08 AM
VBA─ Constants
Data whose values do not change within a certain scope should be declared as constants by
using the Const modifier.
The value of a constant is specified when it is declared (this process is called initialization).
Any attempts to alter the value of a constant then results in a compilation error.
Using constants instead of hard-code literal values is an excellent programming practice. This
makes your code more readable and easier to be modified later on if needed.
The rules for naming a constant are same as those for creating variables. The constant must
have a valid symbolic name and an expression composed of numeric or string constants and
operators (but no function calls).
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
➢ No space, period (.), exclamation mark (!), or the characters @, &, $, # can be used in
the name
➢ The name cannot exceed 255 characters in length
➢ You cannot use Visual Basic reserved keywords as variable name
Syntax
In VBA, we need to assign a value to the declared Constants. An error is thrown, if we try to
change the value of the constant.
MsgBox "Integer is " & MyInteger & Chr(10) & "myDate is " &
myDate & Chr(10) & "myDay is " & myDay
End Sub
Output
*Source: Google
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
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
Operato
r Description
+ Adds the two operands
^ Exponentiation operator
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
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
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
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.
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
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
AND If both conditions evaluate to True then the a<>0 AND b<>0 is False
expression is 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.
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.
Dim a As Integer
a = 10
Dim b As Integer
b=0
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