Problem
At the beginning of each work day, you format and print your pivot table, and several steps are in the process. First, you apply a light PivotTable Style that you use when printing, and then you refresh the data, and, finally, you preview the report. You’re not really a morning person, so you’d like to create a macro that performs all these steps automatically, to make the process easier and faster. This problem is based on the OrderDates pivot table in the Sales11.xlsm workbook.
Solution
Excel programming is done in the Visual Basic for Applications (VBA) language. When manu- ally 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 flexi- ble, 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. The macro recorder also includes many recorded steps that may be unnecessary 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, the macro recorder is a useful tool.
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 exam-
ple, you want the macro to prepare a specific pivot table, so select a cell outside the pivot table, and then select a different worksheet. In the Sales11.xlsmsample file, you can select the Sales Data sheet before you begin recording. You select the OrderDates worksheet and a cell in the pivot table after you begin recording, so those steps will be part of the recorded code.
Recording a Macro
1. On the Ribbon’s Developer tab, in the Code group, click Record Macro.
■ Tip You can also click the Record Macro button at the left side of the Status bar.
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 11-3).
This setting is optional; there are other ways to run the macro after you create it. To run the macro later, using the shortcut, press the Ctrl key, and the Shift key, and then tap the P key. If you use lowercase p as the shortcut, then you press Ctrl and tap the P key, without pressing the Shift key.
Figure 11-3.Ctrl+Shift+P is entered as the macro’s shortcut key.
■ Caution Don’t use a shortcut key that’s the same as the Excel shortcuts you like to use. For example, Ctrl+c copies 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.
4. From the Store Macro In drop-down list, 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 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. Perform the steps you want to record. In the Sales11.xlsmexample, select the Order- Dates worksheet, where the pivot table named PT1 is located. Then, select a cell at the top left of the pivot table. On the Ribbon’s Design tab, open the PivotTable Styles gallery, and then click Pivot Style Light 1. Then, right-click a cell in the pivot table, and click Refresh. Finally, click the Microsoft Office button, point to Print, click Print Pre- view, and click the Close Print Preview button.
■ Note While recording the macro, Live Preview is disabled, so you won’t see a preview when you select a different PivotTable Style.
8. After you complete the steps you want to record, click the Stop Recording button at the left side of the status bar (see Figure 11-4).
Figure 11-4.The Stop Recording button on the status bar Viewing the Recorded Code
1. On Ribbon’s View tab, at the far right, click the top section of the Macros command, to open the Macro dialog box.
2. From the Macros In drop-down list, select This Workbook, or the location in which you stored your macro.
3. In the list of macros, select your macro, and then click Edit.
4. This opens the VBE, where you can see the recorded code.
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 11-5).
Figure 11-5.The VBE shows the recorded code.
■ Note If the Project Explorer is not visible, on the VBE menu, click View, and then click Project Explorer.
6. In the code window at the right, you can see the recorded code. The code is in a proce- dure, 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 com- ments don’t affect the code, but they can make it easier to understand. Some
comments are added by the macro recorder, and you can add your own comments to the code. Lines that don’t start with a single quote mark are the lines of code that con- trol what steps are performed when you run the macro.
■ Note Although the recorder was on when you closed the Print Preview window, that step was not recorded. Some actions cannot be captured by the macro recorder.
8. In the code window, you can edit the existing code or type new code. In this example, you leave the code as it is. To make changes later, you can follow the instructions in Section 11.3.
9. To close the VBE and return to Excel, click the File menu, and then click 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 preparation steps:
1. On the Ribbon’s Developer tab, in the Code group, click Macros.
2. From the drop-down list for Macros In, select the location in which you stored your macro.
3. In the list of macros, select your macro, and then click Run.
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. Or, if you recorded a macro, and did not assign a keyboard shortcut to it, you can use this technique to add a keyboard shortcut later. To add or change a shortcut, fol- low these steps:
1. On the Ribbon’s Developer tab, in the Code group, click Macros.
2. From the Macros In drop-down list, select the location in which you stored your macro.
3. In the list of macros, select your macro, and then click Options.
4. In the Shortcut key box, type a different uppercase or lowercase character.
5. Click OK to close the Macro Options dialog box.
6. Click Cancel to close the Macro dialog box.