Visual Basic recognizes a number of fundamental data types, such as Integer, Currency, String, and Boolean. Each data type takes up a specific amount of memory, allows a specific range of values, and is best suited for storing specific kinds of data. All data must have a type in order for Visual Basic to work with it.
If you don't explicitly declare a type, Visual Basic assigns a default type. For variables, the default type is Variant; for constants, it's the data type that most closely matches the value assigned to the constant.
You can improve macro performance in several ways by specifying data types yourself. You can make your code run faster by specifying the exact type of each variable, as Visual Basic won't have to spend time examining each Variant variable to detect the exact type of the value stored there. You can save memory, because the specific data types use less memory than the Variant type. You can avoid rounding and overflow errors by specifying a data type appropriate to the values you'll be using and the calculations you'll be making. For more information about selecting data types to improve performance, see Chapter 5, "Optimizing for Size and Speed."
The fundamental data types in Visual Basic, including Variant, are shown in the following table. For more information about the specific types, see the following sections of this chapter.
Type name |
Storage size |
Range | |
Integer |
2 bytes |
–32,768 to 32,767 | |
Long |
4 bytes |
–2,147,483,648 to 2,147,483,647 | |
Single |
4 bytes |
– 3.402823E38 to – 1.401298E-45 1.401298E-45 to 3.402823E38 (positive values) | |
Double |
8 bytes |
– 1.79769313486232E308 to 4.94065645841247E – 324 to 1.79769313486232E308 (positive values) | |
Currency |
8 bytes |
– 922337203685477.5808 to | |
String |
1 byte per character |
0 to approximately 65,500 characters | |
Boolean |
2 bytes |
True or False | |
Date |
8 bytes |
January 1, 100 to December 31, 9999 | |
Object |
4 bytes |
Any Object reference | |
Variant |
16 bytes + 1 byte for each character |
Null, Error, any numeric value valid for any numeric data type, or any text, object, or array. |