Variables, data types, constants …

Variables are placeholders within computer memory that are aimed to store values ​​of different types of data, such as: numbers, text, logical values, date/time … Although this is not necessary, for better memory utilization, you need to declare them at the beginning of the program and assign them a data type. Variables that will have fixed values ​​during program execution are called constants.

We declare the variables as follows:

Dim <variable name> [As <data type>] {, <variable name> [As <data type>]}

We wrote the statement with the help of the so-called Backus notation, where the entities are shown in parentheses, and square brackets represent those entities whose occurrence is optional. Wicker brackets frames the entities that could repeat several times. What can we conclude here?

The variable declaration always begins with the Dim command, after which it is mandatory to specify its name. E.g.:

Dim a

Although not mandatory, it is advisable to include the data type after name of the variable. This is done so that the VBA can make better use of the computer’s memory and to make the program better documented. If we want to declare variable as an integer number we should write:

Dim as As Integer

Later, we can give it some value which we enter after the equal sign. For example:

a = 1

In the table you can see the types of data that are used when defining a number of values:

Type Values Description
Byte

0..255

Integer, no sign

Currency

-922,337,203,685,477.5808 do

922,337,203,685,477.5807

Floating point number with 15 characters left and 4 right of decimal point.

Single

-3.402823E38 do

3.402823E38

Floating number of single precision

Double

-4.94065645841247E-324 do

4.94065645841247E-324

Floating number of double precision

Integer

-32,768 do 32,767

Negative and positive integer numbers

Long

-2,147,483,648 do
2,147,483,647

Negative and positive integer numbers of higher precision

You can declare variables and assign them values ​​in the following way:

Dim as As Integer, b As Single
a = 1
b = 3.50

The String type contains the text, so to declare a string variable and assign its value, we do this in the following way:

Dim s1, s2 As String
s1 = “text”
s2 = “”

The values ​​of strings are written under double quotation marks, and if we want to assign an empty string to the variable, we will write twice the double quotation mark character.

Logical variables can have True or False values and we declare them using the Boolean type:

Dim x As Boolean
x = true

To declare a date or time, we use the Date type, and here are examples of declaring such variables:

Dim d As Date
d = #01/01/2019#

Variables that hold constant values ​​in a program are called constants, and we define them with the Const command in the following way:

Const <name of constants> = <value>

E.g.:

Const pi = 3.14

VBA also has intrinsic constants that have predefined values and users can not change them. These are, for example, the values ​​that are used for names of colors.