Microsoft Office Tutorials and References
In Depth Information
Boolean Variables
Boolean variables can hold only the two values True and False . They are widely used in all areas
of programming because you’ll find that you often need to store values that are true/false, yes/no,
or on/off. You assign values to Boolean variables using the VBA keywords True and False . Here’s
an example:
Dim TaskDueToday As Boolean
TaskDueToday = True
Behind the scenes, VBA represents True and False by the values -1 and 0 , respectively. When
treated as a Boolean, any numeric expression will evaluate as True if its value is non-zero.
Date Variables
In VBA, the term “ date” refers to the time of day as well as the calendar date. Dates are stored
internally as floating-point numbers in which the part to the left of the decimal point represents
the number of days since December 30, 1899, with negative numbers representing prior dates. The
part to the right of the decimal point represents the time as a fraction of a day, so that .25 is 6
A.M., .5 is noon, and so on.
The Date data type is designed to hold date values. When you display a date value, the display
format is determined by the system’s short date format setting, and times are displayed as either
12- or 24-hour times, again in accordance with the system settings.
You write literal date values in just about any recognizable format, enclosed in # signs. Here are
some examples:
Dim d1 as Date, d2 As Date, d3 As Date
d1 = #January 31, 2005#
d2 = #31 Jan 05#
d3 = #1/31/2005#
The VBA Editor will automatically convert date literals to a standard format. VBA has a wide range
of tools for working with dates and times. They are beyond the scope of this chapter, but you can
find detailed information on the VBA online documentation.
The Variant Type
The Variant data type is VBA’s default variable type, and also its most flexible. Default means that
if you declare a variable without specifying a type, it is created as a Variant . These declarations
create two type Variant variables:
Dim x
Dim y As Variant
Search JabSto ::




Custom Search