18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introducing Visual Basic for Applications 39<br />

Creating VBA Macros<br />

Excel provides two ways to create macros:<br />

n Turn on the macro recorder and record your actions.<br />

n Enter the code directly into a VBA module.<br />

The following sections describe these methods.<br />

Recording VBA macros<br />

In this section I describe the basic steps that you take to record a VBA macro. In most cases, you can record<br />

your actions as a macro and then simply replay the macro; you needn’t look at the code that’s automatically<br />

generated. If simply recording and playing back macros is as far as you go with VBA, you don’t need to be concerned<br />

with the language itself (although a basic understanding of how things work doesn’t do any harm).<br />

Recording your actions to create VBA code: The basics<br />

Excel’s macro recorder translates your actions into VBA code. To start the macro recorder, choose Developer ➪<br />

Code ➪ Record Macro (or, click the Record Macro icon in the status bar). Excel displays the Record Macro<br />

dialog box, shown in Figure 39.7.<br />

FIGURE 39.7<br />

The Record Macro dialog box.<br />

The Record Macro dialog box presents several options:<br />

n<br />

n<br />

n<br />

Macro Name: The name of the macro. By default, Excel proposes names, such as Macro1,<br />

Macro2, and so on.<br />

Shortcut Key: You can specify a key combination that executes the macro. The key combination<br />

always uses the Ctrl key. You can also press Shift when you enter a letter. For example, pressing<br />

Shift while you enter the letter H makes the shortcut key combination Ctrl+Shift+H.<br />

Store Macro In: The location for the macro. Your choices are the current workbook, your<br />

Personal Macro Workbook (See “Storing macros in your Personal Macro Workbook,” later in this<br />

chapter), or a new workbook.<br />

n Description: A description of the macro (optional).<br />

To begin recording your actions, click OK. When you finish recording the macro, choose Developer ➪<br />

Code ➪ Stop Recording (or click the Stop Recording button in the status bar).<br />

687

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!