What are VBA’s data types?




When I talk about data type, I’m referring to the manner in which a program stores data in memory — for example, as integers, real numbers, or strings. 
Although VBA can take care of these details automatically, it does so at a cost. (There’s no free lunch.) Letting VBA handle your data typing results in slower execution and inefficient memory use. 
For small applications, this usually doesn’t present much of a problem. But for large or complex applications, which may be slow or need to conserve every last byte of memory, you need to be on familiar terms with data types. 
VBA automatically handles all the data details, which makes life easier for programmers. Not all programming languages provide this luxury. 
For example, some languages are strictly typed, which means the programmer must explicitly define the data type for every variable used.
VBA does not require that you declare the variables that you use, but it’s definitely a good practice. You’ll see why later in this chapter.
VBA has a variety of built-in data types. Table 7-1 lists the most common types of data that VBA can handle.

Table 7-1 VBA’s Built-In Data Types
Data Type Bytes UsedRange of Values
Boolean 2True or False
Integer 2–32,768 to 32,767
Long 4–2,147,483,648 to 2,147,483,647
Single 4–3.402823E38 to 1.401298E45
Double (negative) 8–1.79769313486232E308 to –4.94065645841247E-324
Double (positive) 84.94065645841247E–324 to 1.79769313486232E308
Currency 8–922,337,203,685,477.5808 to 922,337,203,685,477.5807
Date 81/1/100 to 12/31/9999
String 1per char Varies
Object 4Any defined object
Variant VariesAny data type
User defined VariesVaries

In general, choose the data type that uses the smallest number of bytes but can still handle all the data you want to store in the variable.
Loop counters are often declared as integers. If you use the counter in order to loop through rows in the worksheet, your program might just error out! Why? Integers cannot be larger than 32,767. 
Beginning with Excel 2007, worksheets have many more rows (1,048,576 to be exact). Instead, declare such loop counters as Long.

0 Response to "What are VBA’s data types?"

Posting Komentar