Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )
We should also comment on what appears and does not appear in the Macro list box. All macros
that we write will appear in the Macros dialog box (as will all recorded macros). However, there
are a few variations. If we give the macro a unique name (within the context given in the "Macros
in" list box), then only the name of the macro will appear in the list box. If the name is not unique,
then it must be qualified by the name of the module in which the macro appears, as in:
Sheet5.ScrollChartTypes
in Figure 4-12. Unfortunately, the first version of a macro with a nonunique name is not qualified.
(Note the presence of another ScrollChartTypes macro in Figure 4-12.)
Note that we can prevent a macro procedure from appearing in the Macros list box by making the
procedure private, using the Private keyword, as in:
Private Sub HideThisMacro()
We will discuss Private and Public procedures in Chapter 6.
TE
Wks_Sort
Wks_Compare
Wks_Print
AM
FL
Y
Finally, if you are like me, you will collect a great many macros over the years. As time goes by,
you may forget the names of some of these macros and thus have trouble finding a macro when
you need it. I would advise you to give some careful thought to creating a consistent naming
convention for macros. I begin the names of all macros with a word that categorizes the macro.
For instance, all of my macros that deal with worksheets begin with the letters Wks, as in:
37 ®
Team-Fly
Part II: The VBA Programming Language
Chapter 5
Chapter 6
Chapter 7
Chapter 8
38
Chapter 5. Variables, Data Types, and Constants
In the next few chapters, we will discuss the basics of the VBA programming language, which
underlies all of the Microsoft Office programming environments. During our discussion, we will
consider many short coding examples. I hope that you will take the time to key in some of these
examples and experiment with them.
5.1 Comments
We have already discussed the fact that comments are important. Any text that follows an
apostrophe is considered a comment and is ignored by Excel. For example, the first line in the
following code is a comment, as is everything following the apostrophe on the third line:
' Declare a string variable
Dim WksName as String
WksName = Activesheet.Name
' Get name of active sheet
When debugging code, it is often useful to temporarily comment out lines of code so they will not
execute. The lines can subsequently be uncommented to restore them to active duty. The
CommentBlock and UncommentBlock buttons, which can be found on the Edit toolbar, will place
or remove comment marks from each currently selected line of code and are very useful for
commenting out several lines of code in one step. (Unfortunately, there are no keyboard shortcuts
for these commands, but they can be added to a menu and given menu accelerator keys.)
5.2 Line Continuation
The very nature of Excel VBA syntax often leads to long lines of code, which can be difficult to
read, especially if we need to scroll horizontally to see the entire line. For this reason, Microsoft
recently introduced a line-continuation character into VBA. This character is the underscore,
which must be preceded by a space and cannot be followed by any other characters (including
comments). For example, the following code:
ActiveSheet.Range("A1").Font.Bold = _
True
is treated as one line by Excel. It is important to note that a line continuation character cannot be
inserted in the middle of a literal string constant, which is enclosed in quotation marks.
5.3 Constants
The VBA language has two types of constants. A literal constant (also called a constant or literal )
is a specific value, such as a number, date, or text string, that does not change, and that is used
exactly as written. Note that string constants are enclosed in double quotation marks, as in
"Donna Smith" and date constants are enclosed between number signs, as in #1/1/96#.
For instance, the following code stores a date in the variable called dt:
39
Dim dt As Date
dt = #1/2/97#
A symbolic constant (also sometimes referred to simply as a constant) is a name for a literal
constant.
To define or declare a symbolic constant in a program, we use the Const keyword, as in:
Const InvoicePath = "d:\Invoices\"
In this case, Excel will replace every instance of InvoicePath in our code with the string
"d:\Invoices\". Thus, InvoicePath is a constant, since it never changes value, but it is not
a literal constant, since it is not used as written.
The virtue of using symbolic constants is that, if we decide later to change "d:\Invoices\" to
"d:\OldInvoices\", we only need to change the definition of InvoicePath to:
Const InvoicePath = "d:\OldInvoices\"
rather than searching through the entire program for every occurrence of the phrase
"d:\Invoices\".
It is generally good programming practice to declare any symbolic constants at the beginning of
the procedure in which they are used (or in the Declarations section of a code module). This
improves readability and makes housekeeping simpler.
In addition to the symbolic constants that you can define using the Const statement, VBA has a
large number of built-in symbolic constants (about 700), whose names begin with the lowercase
letters vb. Excel VBA adds additional symbolic constants (1266 of them) that begin with the
letters xl. We will encounter many of these constants throughout the book.
Among the most commonly used VBA constants are vbCrLf, which is equivalent to a carriage
return followed by a line feed, and vbTab, which is equivalent to the tab character.
5.3.1 Enums
Microsoft has recently introduced a structure into VBA to categorize the plethora of symbolic
constants. This structure is called an enum , which is short for enumeration. A list of enums can be
obtained using my Object Model Browser software. For instance, among Excel's 152 enums, there
is one for the fill type used by the AutoFill method, defined as follows:
Enum XlAutoFillType
xlFillDefault = 0
xlFillCopy = 1
xlFillSeries = 2
xlFillFormats = 3
xlFillValues = 4
xlFillDays = 5
xlFillWeekdays = 6
xlFillMonths = 7
xlFillYears = 8
xlLinearTrend = 9
xlGrowthTrend = 10
End Enum
40