Chapter 2 Variables Data Types Constants
Chapter 2 Variables Data Types Constants
2
Variables, Data types &
Constants
q
What is a Variable & Why do we need variables
q
How to declare a Variable
q
Rules to be followed for a Variable name
q
Implicit and Explicit declaration
q
Forcing Variable Declaration (Option Explicit)
q
Scope of a Variable
q
Assigning values to Variables
q
Data Types
q
Constants
VBA stores data in memory using a Variable. A variable is a name given by you, to which
you assign a piece of data that will be stored in computer’s memory and that can be
retrieved when you need to later in the Macro.
Variables hold values of different Data types that are specified when the variable is
declared.
You declare a variable with declaration statement which contains four key words in
particular order
1. The Dim statement (VBA’s abbreviation for Dimension), which all variable declarations
start with
q
They must begin with an alphabet
q
They must contain only alphabets, numbers or the underscore (_) character – no
spaces
q
They must not exceed 40 characters
q
They must not be a reserved word
Implicit Declaration:
You do not have to declare a variable before using it. You can just include the
statement eg: tempval = 6
However, a problem with doing this is that it can lead to errors in your code
when you misspell the name of the variable in a later statement. For example, if
you refer to it as temval instead of tempval, VBA assumes that temval is a new
variable and assigns it as such. The old variable tempval is still there but no
longer being used. This leads to errors during run time and takes some time to
correct your code.
Explicit Declaration:
To avoid the problem of misnaming variables, you can stipulate VBA to generate
error message (while compilation) whenever it encounters an undeclared
variable. To do this we need to give the below statement in the declaration
section of the module
Option Explicit
Using option explicit is the best practice and helps to avoid run time errors.
Scope of a Variable:
Local Variable:
If you declare a variable within a procedure, only code within that procedure
can access that variable. The scope is local to that procedure. Local variable
remain in existence only as long as the procedure is executing.
Global Variable:
Global variables are declared in the declarations part of a module with the
Global statement and they can be accessed by any code with in the application.
Global Variables exist and retain their values for the life time of the application.
DEMO
A variable can be given a data type that determines the type of data it can store.
This can have an effect on the efficiency of your code. If you don’t specify a data
type then the default data type is Variant
Variant:
A Variant can store all kinds of data whether it’s a text, number, date …A Variant
variable can freely change its type at run time which is not possible for other
data types.
However, using Variant data type reduces the speed of the program. Also
Variant data type occupies more memory compared to the other data types.
Constants:
A Variable’s value may often change during the execution of the Macro.
However a constant is a value in a Macro that does not change during the
execution of the Macro. Essentially, constants are variables that do not
change.
A constant is used to store a fixed value and refer to that value multiple
times in your macro with out hard-coding the actual value every time in
the macro.
q
What is a Variable & Why do we need variables
q
How to declare a Variable
q
Rules to be followed for a Variable name
q
Implicit and Explicit declaration
q
Forcing Variable Declaration (Option Explicit)
q
Scope of a Variable
q
Assigning values to Variables
q
Data Types
q
Constants
Email us – [email protected]
Visit us - https://intellipaat.com