Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 32

Recording a Macro

Оглавление

Here comes the hands-on part. Follow these instructions carefully:

1 Select a cell.Any cell will do.

2 Choose Developer ⇒ Code ⇒ Record Macro, or click the Macro Recording button on the status bar.The Record Macro dialog box appears, as shown in Figure 2-2.FIGURE 2-2: The Record Macro dialog box appears when you’re about to record a macro.

3 Enter a name for the macro.Excel provides a default name (something like Macro1), but it’s better to use a more descriptive name. NameAndTime (with no spaces) is a good name for this macro.

4 Click the Shortcut Key box, and enter Shift+N (for an uppercase N) as the shortcut key. Specifying a shortcut key is optional. If you do specify one, you can execute the macro by pressing a key combination — in this case, Ctrl+Shift+N. Be aware that if you assign a common shortcut key (for instance, Ctrl+C), you lose the normal functionality for that shortcut key; Excel will trigger your macro instead.

5 Verify that the Store Macro In setting is This Workbook.

6 (Optional) Enter some text in the Description box.Some people like to describe what the macro does (or is supposed to do).Figure 2-3 shows the Record Macro dialog box filled in with a name, optional shortcut, and optional description.FIGURE 2-3: The completed Record Macro dialog box.

7 Click OK.The Record Macro dialog box closes, Excel’s macro recorder is turned on, and the Record Macro button’s caption is changed to Stop Recording. From this point, Excel monitors everything you do and converts it to VBA code.

8 Type your name in the active cell.

9 Select the cell below and enter this formula: =NOW()The formula displays the current date and time.

10 Select the formula cell, and press Ctrl+C to copy that cell to the Clipboard.

11 Choose Home ⇒ Clipboard ⇒ Paste ⇒ Values (V).This command converts the formula to its value.

12 Select both the cell with your name and the one with the date and time.

13 Use the controls in the Home ⇒ Font group to change the formatting to Bold and make the font size 16 point.

14 Choose Developer ⇒ Code ⇒ Stop Recording.The macro recorder is turned off.

Congratulations! You just recorded your first Excel VBA macro. You might want to phone your mother and tell her the good news.

Excel VBA Programming For Dummies

Подняться наверх