- Use Recommended PivotTables Feature to Create Pivot Tables Automatically.
- Use VBA to Automate Pivot Table Creation.
Automate PivotTable Creation using Recommended PivotTable
- Place your cursor in a tabular range of data
- Click the
Insert
tab of the ribbon. - Click the
Recommended PivotTables
icon.
Excel shows you a menu of pivot tables it thinks it can create for you, based on the data in your range. When you find one that looks good to you, click it and then click OK to have Excel create it.
Use VBA to Automate PivotTable Creation
The steps you need to follow to create a PivotTable require some effort, and that effort often is redundant. With a small bit of VBA, you can create simple PivotTables automatically.
Two of the most commonly asked questions in Excel concern
- how to get a count of all items in a list, and
- how to create a list of unique items from a list that contains many duplicates.
In this section, we’ll show you how to create a PivotTable quickly and easily that accomplishes these tasks.
Assume you have a long list of names in column A
, with cell A1
as your heading, and you want to know how many items are on the list, as well as generate a list of unique items.
- Select cell
A1
or any cell within your data. - Go to
Insert
tab of the ribbon and clickPivotTable
icon. - The “Create PivotTable” dialog box should automatically pick up the correct range for your data in column
A
and will highlight it in your sheet. - Choose where to place the PivotTable from the “Create PivotTable” dialog box.
- Click OK.
- In the PivotTable Fields pane on the right, drag and drop the field you want to count into the
Values
box. This field should be the column containing the items you want to count. By default, Excel will use theSUM
function to aggregate the values, but you can change it toCOUNT
by configuring the Value Field Settings:- Click on the drop-down arrow next to the field in the
Values
area and selectValue Field Settings
. - Choose
Count
in the “Value Field Settings” dialog box. - Click
OK
to apply the changes.
- Click on the drop-down arrow next to the field in the
- Again, to display a unique item list, drag and drop the field into the
Rows
box. This field should be the column containing the items you want to display as row headings.
The PivotTable will now display the unique items from your list along with a count of how many times each item appears in your list.
What if you want to have a macro perform all those steps for you, creating a PivotTable from any column you feed it?
- Press
Alt+F11
or (Command+F11
) to open the Visual Basic Editor (VBE). - In the VBE window, select
Insert » Module
. - Enter the following code:
Sub GetCount( )
Dim Pt As PivotTable
Dim strField As String
strField = Selection.Cells(1,1).Text
Range(Selection, Selection.End(xlDown)).Name = "Items"
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="=Items").CreatePivotTable TableDestination:="", _
TableName:="ItemList"
Set Pt = ActiveSheet.PivotTables("ItemList")
ActiveSheet.PivotTableWizard TableDestination:=Cells(3, 1)
Pt.AddFields RowFields:=strField
Pt.PivotFields(strField).Orientation = xlDataField
End Sub
- Close the VBE window and save your workbook.
- Before running this code, select the heading of your list and ensure that your list contains no blank cells.
- The code will automatically create a named range of your list, called Items. It will then create the PivotTable based on this named range on a new worksheet.
The next time you have a long list of data, you can simply select its heading and run this macro. All the PivotTable setup work will be done in the blink of an eye.