Page 173 - Excel Data Analysis
P. 173
10 537547 Ch09.qxd 3/4/03 12:13 PM Page 159
AUTOMATING ACTIONS WITH MACROS 9
UNDERSTANDING VBA (CONTINUED)
Boolean
You use a Boolean data type to store a logical value of are predefined as Boolean values in VBA. To assign
True or False.A Boolean data type takes up two them to a variable, you simply specify the value
bytes of data storage. The keywords True and False BoolValue = True.
Date String
You can use the Date data type to store dates and You can use a String data type to store a sequence of
times so that you can use them in calculations. VBA characters. A string can contain any combination of
accepts a date range from January 1, 100 to December letters, numbers, punctuation marks, and spaces. In
31, 9999. Unfortunately, the date range within Excel is order for VBA to recognize the start and stop of your
much smaller — January 1, 1900 to December 31, 9999. string, you must enclose it in quotes — for example:
If you place a date value in an Excel worksheet that is
outside this range, Excel produces an error message. Example:
SampleString = "This is a sample"
The Date data type is an 8-byte value that is stored as a
decimal number. Because dates are numeric, You can declare strings using one of two different types:
calculations can use them. variable length and fixed length. As the names state, you
declare fixed-length strings with a maximum number of
When you specify dates and times in VBA, you enclose characters and variable-length strings with as many as 2
them in pound signs instead of the quotation marks that billion characters.
you use for strings:
To declare a fixed-length string, you need to specify the
Example: string length as part of the definition. When you declare
Const StartDate As Date = #6/12/2001# a string length, the string is always that size, even if you
assign a smaller string to it. For example, you declare a
Object string of 25 characters as follows:
You use the Object data type to define a variable as
one of the objects that are part of the Excel Object Example:
Model. These data types are four bytes in size. Dim FixedString As String * 25
Excel provides an abundance of objects, including On the other hand, variable-length strings have no
Workbook, Window, Chart, and PivotTable. Each of length specified:
the objects provided by the Excel Object Model can be
assigned as a data type — for example: Example:
Dim VarString As String
Example:
Dim chrt as Chart
Dim sheet1 as Worksheet
159