Page 84 - Microsoft Office Excel 2003 Programming Inside Out
P. 84
Microsoft Office Excel 2003 Programming Inside Out
Declaring Variables
Variables are declared within a procedure by using the Dim, or dimension, statement. The
most common usage of the Dim statement is
Dim varname as type
This statement explicitly declares a variable named varname as a variable of type. Multiple
variables can be declared on one line by separating them with a comma, like this:
Dim varname1 as type, varname2 as type, ...
Defining Data Types
When variables are declared, they should be declared as a specific data type. This data type
determines the values that can be stored within the variable and how much memory is required
to store the value. VBA provides different data types to handle numbers, strings, dates and
times, Boolean values, and objects. You can even declare your own data type when needed.
The majority of the data types available in VBA are numeric types, each of which handles a
different range of values with varying amounts of precision. The numeric data types along
with their specifications are listed in Table 4-7.
Table 4-7. Numeric Data Types
Storage
Type Range Description Requirements
Byte 0 to 255 Unsigned, integer 1 byte
number
Currency -922,337,203,685,477.5808 to A signed, fixed-point 8 bytes
Chapter 4
922,337,203,685,477.5807 number with up to 15
digits to the left of the
decimal and 4 digits to
the right; used for
financial or other
calculations requiring a
high degree of precision
Decimal€ +/- 79,228,162,514,264,337,593,543,950,335 Can’t be directly 12 bytes
with no decimal point and +/- declared in VBA;
7.9228162514264337593543950335 with requires the use of a
28 digits behind the decimal point. Variant data type
58
Part 2: Visual Basic for Applications