Microsoft Office Tutorials and References
In Depth Information
Table 5-1. VBA Data Types
variable . For reference, Table 5-1 s hows the complete set of VBA data types, along with the
amount of memory that they consume and their range of values. We will discuss a few of the more
commonly used data types in a moment.
Table 5-1. VBA Data Types
Type
Size in Memory
Range of Values
Byte
1 byte
to 255 0
Boolean
2 bytes
True or False
Integer
2 bytes
32,768 to 32,767 -
Long (long integer)
4 bytes
2,147,483,648 to 2,147,483,647 -
Single(single-
precision real)
4 bytes
Approximately -3.4E38 to 3.4E38
Double(double-
precision real)
8 bytes
Approximately -1.8E308 to 4.9E324
Currency(scaled
integer)
Approximately -922,337,203,685,477.5808
to 922,337,203,685,477.5807
8 bytes
Date
8 bytes
/1/100 to 12/31/9999 1
Object
4 bytes
Any Object reference.
Variable length:10 bytes +
string length;Fixed length:
string length
Variable length: <= about 2 billion (65,400
for Win 3.1) Fixed length: up to 65,400
String
16 bytes for numbers22 bytes
+ string length
Number: same as DoubleString: same as
String
Variant
User-defined
Varies
5.4.1 Variable Declaration
To declare a variable means to define its data type. Variables are declared with the Dim keyword
(or with the keywords Private and Public , which we will discuss later in this chapter). Here
are some examples:
Dim Name As String
Dim Holiday As Date
Dim Age As Integer
Dim Height As Single
Dim Money As Currency
Dim wbk As Workbook
Dim ch As Chart
The general syntax of a variable declaration is:
Dim VariableName As DataType
If a particular variable is used without first declaring it, or if it is declared without mentioning a
data type, as in:
Dim Age
then VBA will treat the variable as having type Variant. As we can see from Table 5-1 this is ,
generally a waste of memory, since variants require more memory than most other types of
variables.
 
 
Search JabSto ::




Custom Search