18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part VI<br />

Programming Excel with VBA<br />

After this variable is defined, you can use the variable Rate instead of the lengthy reference. For example:<br />

Rate.Value = .0725<br />

Besides simplifying your coding, using object variables also speeds your macros quite a bit. I’ve seen macros<br />

execute twice as fast after creating object variables.<br />

Declaring variable types<br />

Usually, you don’t have to worry about the type of data that’s assigned to a variable. Excel handles all these<br />

details behind the scenes. For example, if you have a variable named MyVar, you can assign a number of<br />

any type to it. You can even assign a text string to it later in the procedure.<br />

But if you want your procedures to execute as fast as possible, you should tell Excel in advance what type of<br />

data is going be assigned to each of your variables. Providing this information in your VBA procedure is<br />

known as declaring a variable’s type.<br />

Table 44.1 lists all the data types that are supported by VBA. This table also lists the number of bytes that<br />

each type uses and the approximate range of possible values.<br />

TABLE 44.1<br />

Data Types<br />

Data Type Bytes Used Approximate Range of Values<br />

Byte 1 0 to 255<br />

Boolean 2 True or False<br />

Integer 2 –32,768 to 32,767<br />

Long (long integer) 4 –2,147,483,648 to 2,147,483,647<br />

Single (single-precision floating-point) 4 –3.4E38 to –1.4E–45 for negative values<br />

3 1.4E–45 to 4E38 for positive values<br />

Double (double-precision floating-point) 8 –1.7E308 to –4.9E–324 for negative values<br />

1 4.9E–324 to .7E308 for positive values<br />

Currency (scaled integer) 8 –9.2E14 to 9.2E14<br />

Decimal 14 +/–7.9E28 with no decimal point<br />

Date 8 January 1, 100 to December 31, 9999<br />

Object 4 Any object reference<br />

String (variable-length) 10 + string length 0 to approximately 2 billion<br />

String (fixed-length) Length of string 1 to approximately 65,400<br />

Variant (with numbers) 16 Any numeric value up to the range of a Double<br />

Variant (with characters) 22 + string length Same range as for variable-length String<br />

User-defined (using Type) Number required by Range of each element is the same as the range<br />

elements<br />

of its data type<br />

768

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!