Page 77 - Excel Progamming Weekend Crash Course
P. 77
d540629 ch04.qxd 9/2/03 9:28 AM Page 52
52 Friday Evening
Table 4-1 Continued
Name Type Range Precision
Long Integer -2,147,483,648 to 2,147,483,647 N/A
38
38
Single Floating point -3.4 × 10 to 3.4 × 10 * 6 digits
Double Floating point -1.79 × 10 308 to 1.79 × 10 308 * 14 digits
11
11
Currency Floating point -9.22 × 10 to 9.22 × 10 * 4 digits
* Approximate values.
When you declare a numeric variable, it is assigned the initial value 0.
Note
String Variables
String variables are used to hold text data. A variable-length string can hold any length of
text you put in it, up to some huge maximum of about two billion characters. You don’t
need to worry about the string size because it automatically adjusts to fit its data. The
keyword for declaring this type of variable is String:
Dim Name As String
The other type of string in VBA is a fixed-length string. You set the string’s length when
you declare it, and this remains its maximum capacity. A fixed-length string can be declared
to contain anywhere from 1 to about 64,000 characters. To declare a fixed-length string, use
the String keyword followed by the * (asterisk) symbol and the desired size:
Dim MyFixedLengthString As String * 12
If you assign a string that’s too long to a fixed-length string variable — that is, longer
that the variable’s declared length — the excess characters are lost. For example:
Dim str As String * 5
str = “New York”
The variable now contains “New Y” (remember, spaces count as characters, too).
Some strings, such as “1234,” look like numbers. To VBA, however, it is just
a string and cannot be used in numeric calculations. For example, ZIP codes
Note are usually stored as strings, such as “01345” to ensure that the leading 0
displays.
VBA has some tools for converting strings to numbers; these are covered in
Session 10.
Cross-Ref