Microsoft Office Tutorials and References
In Depth Information
Add a Macro to a Module
Chapter 11: Learning VBA Basics
4 Type Sub , a space, and
then the name of the
Note: Make sure the name you use
is not the same as any existing
macro name in the module.
5 Press Enter.
The VBA Editor adds the
line End Sub to denote
the end of the macro.
If you copied the macro
code from another
source, click Home and
then click Paste, instead.
6 Type the macro
statements between the
Sub and End Sub lines.
As you type a VBA
function, object, property,
or method, the VBA
Editor displays the syntax
in a pop-up box.
If you want to begin your macro with a few
comments — notes that describe what the
macro does — type an apostrophe (’) at the
beginning of each comment line. Also, to
make your code easier to read, indent each
statement by pressing the Tab key at the
beginning of the line — you do not need to
do this for the Sub and End Sub lines. VBA
preserves the indentation on subsequent
lines, so you only have to indent the first line.
Did You Know?
After you enter a statement, VBA
converts keywords to their proper case.
For example, if you type msgbox , VBA
converts it to MsgBox when you press
Enter. By always entering VBA
keywords in lowercase letters, you
can catch typing errors by looking for
those keywords that VBA does not
recognize — in other words, the ones
that remain in lowercase.