Excel 2010 Bible

(National Geographic (Little) Kids) #1

Part VI: Programming Excel with VBA


802


Creating VBA Macros


Excel provides two ways to create macros:

l (^) Turn on the macro recorder and record your actions.
l Enter the code directly into a VBA module.
The following sections describe these methods.


Recording VBA macros

In this section, I describe the basic steps that you take to record a VBA macro. In most cases, you
can record your actions as a macro and then simply replay the macro; you needn’t look at the code
that’s automatically generated. If simply recording and playing back macros is as far as you go with
VBA, you don’t need to be concerned with the language itself (although a basic understanding of
how things work doesn’t do any harm).

Recording your actions to create VBA code: The basics
The Excel macro recorder translates your actions into VBA code. To start the macro recorder,
choose Developer ➪ Code ➪ Record Macro (or, click the Record Macro icon on the left side of
the status bar). Excel displays the Record Macro dialog box, shown in Figure 39.7.

FIGURE 39.7

The Record Macro dialog box.


The Record Macro dialog box presents several options:

l Macro Name: The name of the macro. Excel proposes generic names, such as Macro1,
Macro2, and so on.
l Shortcut Key: You can specify a key combination that executes the macro. The key com-
bination always uses the Ctrl key. You can also press Shift when you enter a letter. For
example, pressing Shift while you enter the letter H makes the shortcut key combination
Ctrl+Shift+H.
Free download pdf