Microsoft Office Tutorials and References
In Depth Information
TABLE 23.1
VBA’s Numeric Data Types
Name
Type
Range
Precision
Size
Byte
Integer
0 to 255
N/A
1 byte
Integer
Integer
–32,768 to 32,767
N/A
2 bytes
Long
Integer
–2,147,483,648 to 2,147,483,647
N/A
4 bytes
Single
Floating point
–3.4 x 10 38 to 3.4 x 10 38 *
6 digits
4 bytes
Double
Floating point
–1.79 x 10 308 to 1.79 x 10 308 *
14 digits
8 bytes
Currency
Floating point
–9.22 x 10 11 to 9.22 x 10 11 *
4 digits
8 bytes
* Approximate values
Why have three integer types and three floating-point types? You can see that the types differ in
terms of the range of values they can hold and, for the floating-point types, their precision or
accuracy. They also differ in the amount of memory they consume. It’s also true that certain operations,
such as division, are faster with the integer types. To be honest, these memory and speed
differences are rarely relevant with today’s fast, memory-laden computers. Choose a numeric data type
based on the range of values the variable will hold. When in doubt, it is always advisable to err on
the side of caution, using a type Long rather than a type Integer , for example. If you try to store
an out-of-range value in a variable, VBA generates an Overflow error.
String Variables
Strings , which is just programmer-talk for text data, represent the second main class of data that VBA
programs work with. VBA provides two types of string variables. A variable-length string can hold
any amount of text up to about 2 billion characters. A variable-length string expands and contracts
automatically to fit the data you put in it, so you do not need to specify the size when declaring it:
Dim City As String
A fixed-length string , on the other hand, has a fixed size you specify when the variable is declared.
This can range from 1 to as many as approximately 64,000 characters. The declaration syntax is as
follows:
Dim FirstName As String * 15
Dim LastName As String * 15
If you assign text that is too long to a fixed-length string variable, the extra part of the text is cut off
and lost.
VBA can figure out that you mean to use s as a number even though it was declared as a string, so
the division is performed properly with the result that i is equal to 8.
Search JabSto ::




Custom Search