Visual Basic and Visual Basic for Applications support the following data types:
Boolean
Indicates the presence of logical data that can contain either of two values, True or False. The keywords True and False are constants that are predefined in VBA, so you can make use of them in your code when you want to assign a value to a Boolean variable, as the following code fragment shows:
var1 = True var2 = False
Many of the properties of ActiveX controls have possible values of True or False. In addition, within programs, Boolean variables often serve as flags to control program flow, as the following example, which toggles (or reverses) the value of myBool within the If...Else...End If construct, shows:
If myBool = False Then myVar = 4 myBool = True Else myVar = 5 myBool = False End If
Storage required
Two bytes
Range
True or False
Default value
False
Byte
The smallest numeric subtype available in VBA. Because only one byte holds a number ranging from to 255 (or 00 to FF in hexadecimal), there is no room for the sign, and so only positive numbers can be held in a Byte data type. Attempting to assign a negative number or a number greater than 255 to byte data results in a runtime error.
Storage required
One byte
Range
0 to 255
Default value
0
Currency
Provides a special numeric format for storing monetary values.
Storage required
Eight bytes
Range
–922,337,203,685,477.5808 to 922,337,203,685,477.5807
Default value
0
Date
Contains a specially formatted number that represents the date or time.
Storage Required
Eight bytes
Range
1 January 100 to 31 December 9999
Default value
00:00:00
Decimal
A variant subtype (and not a separate data type) that contains decimal numbers scaled by a power of 10. Variants of subtype Decimal can only be created by the CDec conversion function.
Storage required
14 bytes
Range
With no decimal point: +/– 79,228,162,514,264,337,593,543,950,335
With up to 28 decimal places: +/– 7.9228162514264337593543950335
Default value
0
Double
Stores a double precision floating point number; basically, it's the industrial strength version of the Single data type.
Storage required
Eight bytes
Range
Negative values: –1.79769313486232E308 to –4.94065645841247E-324
Positive values: 1.79769313486232E308 to 4.94065645841247E-324
Default value
0
Integer
A whole number that ranges from –32,768 to 32,767. One bit represents the sign (either positive or negative). Attempting to assign a value outside its range results in a runtime error.
Storage required
two bytes
Range
–32,768 to 32,767
Default value
0
Long
A signed integer stored in four bytes of memory. One bit represents the sign.
Storage required
Four bytes
Range
–2,147,483,648 to 2,147,486,647
Default value
0
Object
Contains a reference to (i.e., the address of) an object. The object can be an OLE automation object such as an ActiveX component, or it can be a class object within your project. When you use the generic Object data type, rather than a more specific object type, you automatically use late binding. For more information about using the Object data type, see Chapter 4.
Storage required
Four bytes
Range
Any object reference
Default value
Nothing[1]
[1] Nothing and Empty are special Variant data subtypes and do not have the same meaning. For more information, see Section 3.3 later in this chapter.
Single
A single precision number that represents fractional numbers, numbers with decimal places, or exponential numbers.
Storage required
Four bytes
Range
Negative values: –3.402823E38 to –1.401298E-45
Positive values: 1.401298E-45 to 3.402823E38
Default value
0
String (fixed length)
Popular in VB applications when memory and disk storage was at a premium and programmers had to spend most of their time optimizing the size of applications, fixed-length strings are now rarely used. To declare a fixed-length string, use the syntax:
Dim|Private|Public varname As String * stringlength
Storage required
Length of string
Range
1 to 65,400 characters
Default value
A number of spaces equal to the length of the string
String (variable length)
String data type that expands and contracts dynamically to store as many characters as required, up to somewhere in the neighborhood of two billion. To declare a variable-length string, simply use the String keyword:
Dim variablename As String
VBA includes many useful intrinsic functions for handling and manipulating string data. The list of string functions has been expanded in VB6, as many of the string-manipulation functions introduced into VBScript have now made their way into the full language.
Storage required
10 + length of the string
Range
0 to 2 billion characters
Default value
Zero-length string ("")
User-defined type
A user-defined type allows you to create a single data type consisting of a combination of intrinsic VB data types, arrays, objects, or other user-defined types. User-defined types are created using the Type statement. The following snippet shows how to declare a user-defined type:
Type udtCustomer Name As String Code As Long Orders(20) As udtOrders RenewalDate As Date End Type
User-defined types are important data structures in VB and are often essential when interfacing with the Windows API. For more information on user-defined types, see Section 3.6 later in this chapter and the entry for the Type statement in Chapter 7.
Storage required
Sum of storage size of the individual elements
Range
Same range as data type of individual elements
Default value
The default value of the individual elements
Variant (character)
The variant string subtype is very much like a variable-length string data type. All VB string functions can accept variant strings, and many have two versions that return either a strongly typed string data type or a variant string subtype. For example, the Left function—which returns the leftmost n characters of a string—has two variations, Left$ (which returns a string data type) and Left (which returns a variant of subtype string).
Storage required
22 bytes + length of string
Range
Same as variable length string
Default value
Empty[2]
[2] Nothing and Empty are special Variant data subtypes and don't have the same meaning. For more information, see Section 3.3 later in this chapter.
Variant (numeric)
The variant numeric subtype holds any numeric value. As with all variant data, memory allocation changes dynamically to accommodate the numeric value. The variant also includes a special Decimal subtype that doesn't have an intrinsic equivalent, and allows you to hold very large numbers in a variety of formats.
Storage required
16 bytes
Range
Same as Double
Default Value
Empty[3]
[3] Nothing and Empty are special Variant data subtypes and do not have the same meaning. For more information, see Section 3.3 later in this chapter.