If you’ve ever repeated the same actions multiple times in a spreadsheet, you’ll benefit from recording a macro to automate those tasks. But, what if what you regularly create are PivotTables? In this article, we will explore ways a macro can help you create PivotTables and then share three quick ways to add a macro button to your worksheets – for PivotTables or anything else!

Create a PivotTable Macro

Unless you are turning the exact same format and type of data into PivotTables over and over, it might not make sense to record a macro that only works against one spreadsheet. However, there are some ways a macro can be really helpful when adjusting your PivotTables to get them to look consistent and useful. Here are two tips to make creating and editing PivotTables easier.

Turn your data into a table

If not already, turn your data into a table instead of raw data (and give the table a name in the Properties group on the Design tab). Tables offer you the great benefit of organizing information in a way that is easily updatable. For example, if you have created a Chart based on a table and then add rows of information to the table, the chart will automatically refresh, as it has done in example A.

In example B, the same two highlighted rows of information were added to the data, but the chart did not refresh. You would have to click the Select Data button and manually add the new rows to the data range.

A screenshot of a spreadsheet

Description automatically generated

The same convenience applies to PivotTables. If your information is in a Table, select the Table name when you are creating it and then you won’t have to constantly change the Table’s range when you add rows to the data.

A screenshot of a computer

Description automatically generated

Determine your “common” settings

Do you always turn off Subtotals? Do you have a custom, corporate PivotTable Style that you always select each time you create a new PivotTable?

To take advantage of macros and still leave yourself enough flexibility to use the macro on different sets and layouts of data, think through all those extra clicks you perform for almost every PivotTable you create. Then, to create your time-saving PivotTable Macro follow these steps:

1.     Put your mouse cursor inside the data table before you record the macro.

2.     Start the macro recording

A screenshot of a computer

Description automatically generated

HOT TIP! If you want the macro to be available in other worksheets, choose Personal Macro Workbook for the store destination.

3.     Click Insert > PivotTable

4.     Make any of your usual changes to the Create PivotTable dialog box, such as changing where it will be placed.

Note that because your data is in a table, and that you clicked inside the table before you created the PivotTable, the Table/Range defaults to the data you want, even if the area is different each time you run the macro.

5.     Click OK. Excel opens the PivotTable Fields pane and puts a placeholder Report in the place you defined in the Create PivotTable dialog box.

6.     BEFORE you choose fields and create your report, make the other layout changes you would like captured in your macro, first. These changes might include:

7.     Changing Subtotal and Grand Total defaults

8.     Turning Row and Column headers off or on

9.     Choosing a PivotTable style

A screenshot of a computer

Description automatically generated

10.     Once all of your layout changes have been captured, STOP the macro.

Now you can create your report to meet the specific needs of the data, but those common annoying clicks will be handled in a flash the next time you are ready to create a new PivotTable. Just click anywhere inside the table that contains your data, and then run your macro.

AND, because your source data is in a table, just add new rows as needed to your data, hit Refresh on your PivotTable and the new information will automatically appear.

Add a Macro Button To Your Worksheets

Once you have your PivotTable macro, how do you access it?

The easiest way is to add a macro button to your spreadsheet, for which Excel offers three straightforward methods:

  • Add a button to the Quick Access Toolbar;
  • Add a form control button; or
  • Add an ActiveX control button.

In this example, we’ll add a button for the macro titled “CreatePivotTable” since we just created that macro. Before you can use any of these methods, however, you must have the Developer tab available on the ribbon.

Show the Developer Tab

Look at the far right of your Excel ribbon. If there is no “Developer” tab, then add it:

1.     From the “File” menu, choose “Options.”

2.     Choose “Customize the Ribbon.”

3.     Click the checkbox for “Developer.”

Add a Button to the Quick Access Toolbar

The simplest way to add a macro shortcut is to add it to the Quick Access toolbar, which is displayed in the Excel window title bar.

1.     Right-click a blank area of the ribbon.

2.     Choose “Customize Quick Access Toolbar.”

3.     From the dialog box that appears, choose “Macros” for “Choose commands from:” and select your macro from the list.

A screenshot of a computer

Description automatically generated

4.     Click “Add >>” and “Save.”

Although this is the easiest method, it is also the least flexible. The shortcut can’t appear anywhere except in the Quick Access Toolbar, and it can do nothing but run a macro.

Add a Form Control Button

More flexible than the Quick Access toolbar, a form control button can go anywhere in your workbook, and you control the position, size, text, and style.

1.     Choose the Developer”ribbon.

2.     Choose Insert > Button (Form Control)

A screenshot of a computer

Description automatically generated

3.     Click and drag a rectangle for the button.

4.     When you release your mouse button, a macro dialog box will appear.

A screenshot of a computer

Description automatically generated

5.     Click the name of the macro that you wish to run when the user presses the button, then choose “OK.”

6.     Excle will draw the button with the name “Button 1,2” etc. To edit the button text, right-click on the button and select Edit Text.