Using a Toolbar Button: To run a macro, you can add a button to a toolbar:
1. On Excel’s Worksheet menu, choose Tools ➤Customize.
2. On the Commands tab, in the Categories list, select Macros.
3. From the Commands list, drag the Custom Button to an existing toolbar.
4. Click the Modify Selection button, and choose Assign Macro.
5. Select a macro from the list, and click OK.
6. Click Close to close the Customize dialog box.
7. To run the macro, click the toolbar button.
13.2. Recording a Macro While Printing a Pivot Table
Problem
You frequently format and print your pivot table, and there are several steps in the process.
First, you apply an AutoFormat, then you refresh the data, and finally you preview the report. You would like to create a macro that performs these steps automatically to make the process easier and faster.
Solution
Excel programming is done in the Visual Basic for Applications (VBA) language. When manually changing or creating a pivot table, you can use Excel’s macro recorder to create code as you work. Later, you can run the macro as recorded, or adjust the code to make it more flexible, enabling it to run correctly if the worksheet data or layout changes.
The macro recorder doesn’t create ideal code. It records what you do with the mouse and keyboard as you manually perform a task, but occasionally, it’s unable to record one or more steps, so the resulting code is incomplete. It also includes many recorded steps that may not be necessary in the final code, such as multiple clicks on the scroll bar but- ton to move down the worksheet. However, for simple tasks, or for learning about Excel’s object model and programming syntax, it’s a useful tool.
1 3 . 2 ■ R E C O R D I N G A M A C R O W H I L E P R I N T I N G A P I V OT TA B L E 248
Figure 13-1. Ctrl+Shift+P is entered as the macro’s shortcut key.
Before You Begin Recording
When creating a macro, you can select a range as the first step in the macro and operate on that range, or you can record steps that operate on the currently selected range. In this example, you want the macro to prepare a specific pivot table, so select a cell outside the pivot table, and then select a different worksheet. You’ll select the pivot table’s worksheet and a cell in the pivot table after you begin recording.
Recording a Macro
1. Choose Tools ➤Macro ➤Record New Macro.
2. Type a one-word name for the macro, for example, Prepare_Pivot.
3. If you want to run the macro by using a keyboard shortcut, type an upper- or lowercase letter in the Shortcut key box. In this example, uppercase P is used (see Figure 13-1). This setting is optional; there are other ways to run the macro after you’ve created it.
■ Caution Don’t use a shortcut key that’s the same as the Excel shortcuts you like to use. For example, Ctrl+c will copy the selection in Excel. If you use Ctrl+c as your macro shortcut, it will overwrite Excel’s copy shortcut while the workbook with the macro shortcut is open. However, the shortcut keys are case sensitive;
you could hold the Shift key, and use an uppercase C as your shortcut, and it won’t overwrite the Copy shortcut.
1 3 . 2 ■ R E C O R D I N G A M A C R O W H I L E P R I N T I N G A P I V OT TA B L E 249
Figure 13-2. The Stop Recording toolbar, with the Stop Recording button at the left and the Relative Reference button at the right
4. From the Store macro in dropdown, select a location in which to save the macro.
In this example, the macro is specific to the active workbook, so you would select This Workbook. If it’s a macro that you want to use in many workbooks, you could store it in your Personal Macro Workbook. The third option is to store it in a new workbook.
5. To help you, or other users, understand what the macro does, you can enter a brief message about the macro’s purpose in the Description box.
6. Click the OK button to start recording the macro.
7. The Stop Recording toolbar should appear, with two buttons: Stop Recording and Relative Reference (see Figure 13-2).
8. Perform the steps that you want to record. In this example, select the pivot table’s worksheet, then select a cell at the top left of the pivot table. On the Excel Work- sheet menu, choose Format ➤AutoFormat, select Report 6 AutoFormat, and click OK. Then, on the PivotTable toolbar, click the Refresh Data button. Finally, on the Excel Standard toolbar, click the Print Preview button, and click the Close button.
9. When you’ve completed the steps that you want to record, click the Stop Recording button on the Stop Recording toolbar.
Viewing the Recorded Code
1. On Excel’s Worksheet menu, choose Tools ➤Macro ➤Macros.
2. From the Macros in dropdown list, select This Workbook, or the location in which you stored your macro.
3. In the list of macros, select your macro, then click the Edit button (see Figure 13-3).
1 3 . 2 ■ R E C O R D I N G A M A C R O W H I L E P R I N T I N G A P I V OT TA B L E 250
Figure 13-4. The Visual Basic Editor shows the recorded code.
4. This opens the Visual Basic Editor (VBE), where the recorded code was stored.
5. At the left, you should see the Project Explorer, which lists the open Excel files. Your active workbook is in the list, with its modules and Excel objects listed. The recorded code was stored in a module, which is highlighted in the list (see Figure 13-4).
■ Note If the Project Explorer is not visible, on the VBE menu choose View ➤Project Explorer.
Figure 13-3. Select your macro in the list, then click the Edit button.
1 3 . 2 ■ R E C O R D I N G A M A C R O W H I L E P R I N T I N G A P I V OT TA B L E 251
6. In the code window at the right, you can see the recorded code. The code is in a procedure, which starts with a line that contains the word Sub, followed by the name you gave the macro and a set of parentheses.
7. Following the Sub line are comment lines that start with a single quote mark. The comments don’t affect the code, but can make it easier to understand. Lines that don’t start with a single quote mark are the lines of code that control what steps are performed when you run the macro.
■ Note Although the recorder was on when you closed the Preview window, that step was not recorded.
8. In the code window, you can edit the existing code or type new code.
9. To close the VBE and return to Excel, choose File ➤Close and Return to Microsoft Excel.
Running a Macro
When you’re ready to print the pivot table, you can run your macro to perform the prepa- ration steps:
1. On Excel’s Worksheet menu, choose Tools ➤Macro ➤Macros.
2. From the Macros in dropdown list, select the location in which you stored your macro.
3. In the list of macros, select your macro, then click the Run button (see Figure 13-3).
Changing a Macro Keyboard Shortcut
After creating a macro that runs from a keyboard shortcut, you may want to change the letter used in the shortcut. To do so, follow these steps:
1. On Excel’s Worksheet menu, choose Tools ➤Macro ➤Macros.
2. From the Macros in dropdown list, select the location in which you stored your macro.
3. In the list of macros, select your macro, then click the Options button (see Figure 13-3).
4. In the Shortcut key box, type a different character.