Microsoft Office Tutorials and References
In Depth Information
Declaring a Variable for Dates and Times
declaring a Variable for dates and Times
The Date data type is worth an extra look, because it is the data type with which variables for both
dates and times can be declared. You can assign values to a date variable by enclosing them in the
# number sign character, with the value being recognizable to Excel as either a date or time. For
example:
myDate = #09 October 1958#
or
myDate = #October 9, 1958#
or
myTime = #9:10 PM#
or
myTime = #10/9/1958 9:10:00 PM#
When entering dates, get into the good habit of entering the year as a full
fourdigit number. The year 2029 is the dividing line in VBA for two-digit years
belonging to either the twentieth or twenty-first centuries. All two-digit years from 00
to and including 29 are regarded as belonging to the 2000s, and 30 to 99 are
regarded as belonging to the 1900s. For example, the expression 10/10/29 in Excel
is October 10, 2029, but 10/10/30 is regarded by Excel as October 10, 1930.
declaring a Variable with the proper data Type
As you become more familiar with VBA, you’ll notice that different developers have their preferred
writing styles when declaring variables. For example, you can declare several variables on one line,
each separated by a comma, like this:
Dim myValue1 as Integer, myValue2 as Integer, myValue3 as Integer
There is nothing wrong with that construction, but be careful not to make this common mistake:
Dim myValue1, myValue2, myValue3 as Integer
If you do not specify a data type after a variable name, such as in the latter case with myValue1 and
myValue2 , VBA will assign the Variant data type. Only the Value3 variable has been specified the
Integer data type. Variant is a catch-all data type that is the most memory-intensive, and the least
helpful in understanding the purpose of its associated variables if anyone else should read your code.
Search JabSto ::




Custom Search