Microsoft Office Tutorials and References
In Depth Information
Using Assignment Statements
type is also useful for storing time-related data. In VBA, you specify dates and
times by enclosing them between pound signs (#).
The range of dates that VBA can handle is much larger than Excel’s own date
range, which begins with January 1, 1900. Therefore, be careful that you
don’t attempt to use a date in a worksheet that lies outside Excel’s
acceptable date range.
Here are some examples of declaring variables and constants as Date data types:
Dim Today As Date, StartTime As Date
Const FirstDay As Date = #1/1/2003#
Const Noon = #12:00:00#
Date variables display dates according to your system’s short date format,
and times appear according to your system’s time format (either 12 or 24
hours). You can modify these system settings by using the Regional Settings
option in the Windows Control Panel.
Using Assignment Statements
An assignment statement is a VBA instruction that evaluates an expression and
assigns the result to a variable or an object. An expression is a combination of
keywords, operators, variables, and constants that yields a string, number, or object.
An expression can perform a calculation, manipulate characters, or test data.
If you know how to create formulas in Excel, you’ll have no trouble creating
expressions in VBA. You can assign a VBA expression to a variable or use it as a
property value. VBA uses the equal sign (=) as its assignment operator. Note the following
examples of assignment statements (the expressions are to the right of the equal sign):
x = 1
x = x + 1
x = (y * 2) / (z * 2)
SheetHasCharts = True
Expressions often use functions. These can be VBA’s built-in functions, Excel’s
worksheet functions, or custom functions that you develop in VBA. I discuss VBA’s
built-in functions later in this chapter.
Search JabSto ::




Custom Search