Author: Oscar Cronquist Article last updated on February 08, 2023

This article explains the basics of Excel's pivot table, I have included VBA code for the most common actions.

1. What is a Pivot Table?

A pivot table allows you to summarize huge amounts of values amazingly fast in groups and sub-groups you specify. You can then analyze the data with ease, compare values by date or by group and see important trends. It is one of the best and most powerful Excel features and also one of the least known.

pivot table2

Pivot table charts is a great tool for visualizing your data.

pivot table3

Slicers allow you to quickly filter data, however, the report filter has the same functionality but perhaps not as elegant.

pivot table slicers

The following article shows you how to analyze pivot table data:

Recommended articles

Analyze trends using pivot tables
Table of Contents Introduction to pivot tables Create pivot table Group data Analyze data (pivot table) Compare performance, year to […]

Back to top

2. Prepare source data

Before you build your first pivot table make sure your data source table follows some simple rules.

  1. Use unique header names for all your columns.
  2. Make sure you have no blank cells.
  3. Check your spelling. If you have one cell "West" and another "Westt" they will both show up in the pivot table. You can correct this later.
  4. Convert your data source to an excel defined table (optional).

Back to top

3. Rearrange values

This picture below shows you a table with bad data structure, you can't use it in a pivot table.

pivot table

The table below is much better, all values in this table are not shown for obvious reasons. A couple of things are missing though, can you see it? Unique table header names and the data table is not an excel defined table.

You don't have to use an excel defined table but it will make it a lot easier if you add more values later on to your table. An excel defined table is dynamic and it will save you time not needing to adjust the pivot table source range.

pivot table1

I have made a macro/udf that can help you rearrange your data, see these posts:

Recommended articles

Normalize data [VBA]
To be able to use a Pivot Table the source data you have must be arranged in way that a […]

Recommended articles

Prepare data for Pivot Table – How to split concatenated values?
This article demonstrates a macro that allows you to rearrange and distribute concatenated values across multiple rows in order to […]

Back to top

4. Use an Excel Table as a data source

Why do you want to use an Excel Table as a data source to your pivot table? You don't have to but it will make your life easier whenever you want to add or delete records to your data set.

The Excel table adjusts automatically to new data and this saves you time since you don't need to update the data source cell reference each time.

Here is how you convert a generic data set to an excel defined table:

  1. Select a cell in your data table.
  2. Go to tab "Insert" on the ribbon.
  3. Press with left mouse button on "Table" button.

You can also use these shortcut keys: CTRL + T

This dialog box shows up.

create an excel defined table

Excel finds the entire data table automatically =$A$1:$C$48, do make sure this is correct.

My table does not have table headers so I don't select the "My table has headers" check box.

Press with left mouse button on the OK button.

create an excel defined table1

Excel has now converted your data table to an excel defined table. It has also inserted new column headers, see picture above. I don't want to use these table header names as they are not descriptive of each column.

create an excel defined table2

This table is much better, later on these table header names will make it much easier for us when working with the pivot table.

Learn how to change data source with a drop-down list:

Recommended articles

Change PivotTable data source using a drop-down list
In this article, I am going to show you how to quickly change Pivot Table data source using a drop-down […]

Learn more about excel tables:

Recommended articles

How to use Excel Tables
An Excel table allows you to easily sort, filter and sum values in a data set where values are related.

Back to top

5. How to build a pivot table

  1. Select a cell in your data table.
  2. Go to tab "Insert" on the ribbon.
  3. Press with left mouse button on the "Pivot table" button.

This dialog box appears.

create pivo table

It uses the excel defined table name in the first field, very good. It also allows you to choose where you want your new pivot table. I am going to place the pivot table on a new sheet. Press with left mouse button on the OK button.

create pivot table1

Excel creates a blank pivot table for us on a new worksheet, see picture above.

5.1 How to insert a Pivot Table programmatically

This is what the macro recorder returns while inserting a pivot table.

Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Source data!R1C1:R365C7", Version:=6).CreatePivotTable TableDestination:= _
"Sheet17!R3C1", TableName:="PivotTable7", DefaultVersion:=6
Sheets("Sheet17").Select
Cells(3, 1).Select

The following article shows you how to refresh a pivot table automatically:

Recommended articles

Auto refresh a pivot table
In a previous post: How to create a dynamic pivot table and refresh automatically I demonstrated how to refresh a pivot […]

Back to top

6. How to configure a Pivot table

The pivot table on the worksheet is blank and it tells us "To build a report, choose fields from the PivotTable Field List".

You can find our fields in the blue box named 1, see picture below. The fields are Region, Date and Amount the same as your header names in your data source table, now you understand why it is important to name your data source headers.

create pivot table2

The way this works is that you can press and hold with left mouse button on one of the fields and then drag it to an area. The areas are Filters, Columns, Rows and Values and I have drawn a blue box around them with the name 2, see above picture.

Drag Region to Filters, Date to Rows and Amount to Values, see picture below. Excel is trying to help me out here, it automatically grouped my dates into months se column "Row Labels". Don't worry, I will show you later on how to group and ungroup dates.

create pivot table3

6.1 How to move a column header to the Filters area programmatically - Pivot Table

This is what the macro recorder returns when I drag Region to Filters area.

With ActiveSheet.PivotTables("PivotTable3").PivotFields("Region")
.Orientation = xlPageField
.Position = 1
End With

6.2 How to move a column header to the Columns area programmatically - Pivot Table

Here is the output when I drag Region to Columns area.

With ActiveSheet.PivotTables("PivotTable3").PivotFields("Region")
.Orientation = xlColumnField
.Position = 1
End With

6.3 How to move a column header to the Rows area programmatically - Pivot Table

This happens while recording Date to Rows area.

With ActiveSheet.PivotTables("PivotTable3").PivotFields("Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").AutoGroup

6.4 How to move a column header to the Values area programmatically - Pivot Table

Finally Amount to Values area.

ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Amount"), "Sum of Amount", xlSum

Learn how to use hyperlinks in a pivot table:

Recommended articles

Use hyperlinks in a pivot table
Sean asks: Basically, when I do a refresh of the data in the "pivotdata" worksheet, I need it to recognise […]

Back to top

6.5 Report Filters

The report filter allows you to select a subset of your data. In this case I want to work with values in region "West". Simply press with left mouse button on the arrow and select a Region. Press with left mouse button on OK. See animated picture below.

pivot chart - report filter

If you are interested in the vba code for this action here it is.

ActiveSheet.PivotTables("PivotTable3").PivotFields("Region").ClearAllFilters
ActiveSheet.PivotTables("PivotTable3").PivotFields("Region").CurrentPage = _
"West"

Back to top

6.6 Column Labels

Press with left mouse button on and drag Region to Columns area.

pivot table - columns area

Unique values from column Region in your source data table shows up horizontally on the pivot table (cell range C4:E4, pic above).

Back to top

6.7 Row Labels

The following picture shows Region in Rows area and Date in Filters area.

pivot table - row labels

Back to top

6.8 Values

The last area is Values. You are not limited to numbers (Amount) here, you can drag Dates or Region here also. Notice that you can't sum text values but you can count them.

Learn how to link a combo box with a pivot table:

Recommended articles

Populate a combobox with values from a pivot table [VBA]
In this post I am going to demonstrate two things: How to populate a combobox based on column headers from […]

Back to top

7. Pivot table features

Here is a list of the most used functionalities.

7.1 Summarize and analyze

The pivot table is phenomenal at processing huge amounts of data very quickly. Since calculations are done in almost no time you can easily drill down in every detail very quickly.

I have shown you earlier in this article that if you drag a field to Values area, the pivot table sums your values into groups depending on what specific fields you have in the Columns and Rows area.

It also allows you to see trends in your numbers, follow this example. Drag "Amount" once again to Values area, you should now have two "Amount" there.

pivot table - analyze

Left press with left mouse button on the down pointing arrow next to the second "Amount" and then press with left mouse button on "Value Field Settings...". Press with mouse on tab "Show Values As"

pivot table - analyze2

Select Date in Base field: and (previous) in Base item:, see picture above. Press with left mouse button on OK

pivot table - analyze3

There is now a new column next to the sums. It shows the % difference compared to the previous values. The number in cell B8 compared to the number in cell B7 is -4.86% less. (8410/8840) -1 = -4.86%

Learn to build a pivot table calendar:

Recommended articles

Pivot Table calendar
This article demonstrates how to build a calendar in Excel. The calendar is created as a Pivot Table which makes […]

Back to top

7.2 Count

The following picture shows Region in both Values area and Rows area. Date in Columns area grouped by month.

pivot table - Values area2

Back to top

7.3 Unique distinct list

You can use the a pivot table to extract a unique distinct list from a column in a large list. The picture shows countries, simply drag Country to Rows area.

pivot table - Unique distinct values

Back to top

7.4 Extract unique distinct records

Here is a table with many duplicate records.

  1. Select a cell in the table above.
  2. Go to tab "Insert" and press with left mouse button on "Pivot table" button
  3. Place the pivot table somewhere on your worksheet/workbook.
  4. Drag "Name", "City" and "State" to "Row Labels" field
  5. Also drag "Name" to "Values" field

  6. Values are not on the same row which is confusing. Go to tab "Design" on the ribbon, press with left mouse button on "Report Layout" button and then "Show in tabular form"

  7. To show all values, go to tab "Design" and press with left mouse button on "Report Layout". Then press with left mouse button on "Repeat All Item Labels"
  8. The last thing to do is to hide "Subtotals", press with left mouse button on "Do Not Show Subtotals" button.

Here is a picture of the final table, it shows you unique distinct records. In other words, duplicate records are removed from the source data table.

It is possible to extract unique distinct records using a formula, read this post:

Recommended articles

Filter unique distinct records
Table of contents Filter unique distinct row records Filter unique distinct row records but not blanks Filter unique distinct row […]

Back to top

7.5 Count unique distinct values

Excel 2013 and later versions allows you to count unique distinct values. The following picture shows you data table, the scenario is this:

How many unique distinct products did Jennifer sell in region "South" and in January 2011?

I have highlighted the values below that match above criteria.

For this to work you need to enable a check box before creating a new pivot table.

Press with left mouse button on OK button. To be able to filter month January and not individual dates only, drag "Date" to Rows area.

There are now two "Date" fields in Rows area, the other is "Date (Month)". Drag "Date (Month)" from Rows area to Filters area and drag "Date" back to top area. It now looks like this:

Drag "Region" and "Sales person" to Filters area. Drag "Product" to Rows and Values area.

Press with mouse on "Count of Products" then press with left mouse button on "Value Field Settings..."

Select "Distinct Count" and press with left mouse button on "OK" button

Now let's filter the pivot table, press with left mouse button on filter "arrow" next to All and select "January", "South" and "Jennifer". Press with left mouse button on OK button.

Here is the final pivot table:

There are two unique distinct products shown next to "Grand Total".

Related article:

Recommended articles

Count unique distinct values in an Excel Pivot Table
ExcelBeginner asks: I have a small problem that I am not sure on how to solve. I now have a […]

Back to top

7.6 Sort data

It is possible to sort almost anything with data in a pivot table. This picture shows a pivot table and I want to sort column East from smallest to largest. Press with right mouse button on on a cell in a column you want to sort. Press with mouse on "Sort" and then sort Smallest to Largest.

pivot table - sort

Recommended articles

Macro creates links to all sheets, tables, pivot tables and named ranges
This article demonstrates a macro that automatically populates a worksheet with a Table of Contents, it contains hyperlinks to worksheets, […]

Back to top

7.7 Group data

A pivot table allows you to group dates. You can group dates (and time) by seconds, minutes, hours, days, months, quarters and years.

Press with right mouse button on on a column or row you want to group, then press with left mouse button on group and this dialog box appears.

pivot table - group dates

Below Group is Ungroup and I don't think I have to explain that.

pivot table - ungroup

Recommended articles

Disable autofit column widths for Pivot table
I read this interesting article Quick Trick: Resizing column widths in pivot tables on the Microsoft Excel blog. It is […]

Back to top

7.8 Multiple levels

You can rearrange the pivot table so it has multiple levels on Rows area or Columns area or both. Drag more than one field to an area to create multiple levels.

pivot table - multiple levels

The picture shows you data grouped by quarter and then by region. You can also drag fields in an area to sort them, the order is important.

pivot table - multiple levels1

The Date and Region field switched places.

Back to top

7.9 See data behind a pivot table cell

To see the data behind a pivot table cell just double press with left mouse button on a cell that interests you and excel creates a new sheet with the corresponding data shown.

pivot table - data

Back to top

7.10 Row and column grand totals

If you don't need the grand totals, turn them off by going to tab "Design", press with left mouse button on "Grand Totals" button and pick a setting you want. The choices are:

  • Off for Rows and Columns
  • On for Rows and Columns
  • On for Rows only
  • On for Columns only

Back to top

8. Insert a pivot chart

A pivot chart helps you visualize Pivot Table data.

  1. Make sure you have any cell selected on your Pivot Table
  2. Go to tab "Analyze" on the ribbon.
  3. Press with left mouse button on "Pivot chart" button.

From here you can pick a variety of charts, the preview helps you in your decision.

pivot charts

The chart will change if you apply a filter or sort pivot table.

pivot chart - filter

Back to top

9. Slicers

Excel 2010 and later excel versions lets you insert slicers, they are no different than the Report Filter except that they look different and take up more space on your worksheet.

Go to tab "Insert" on the ribbon and press with left mouse button on the "Slicers" button.

pivot table - insert slicers

From here you can pick a field, I will select Region and then press OK button. The slicer appears on your worksheet, you may need a new location for it.

pivot table - insert slicers1

Back to top

10. Refresh a pivot table

If you add, remove or edit values in your source data table you must update the pivot table to reflect the changes made, every time. This is easy to forget, here is how to do it.

Press with right mouse button on on a pivot table cell.

pivot table - refresh

Press with left mouse button on "Refresh".

You can automate this with a macro.

Private Sub Worksheet_Activate()
Sheets("Pivot table").PivotTables("PivotTable1").RefreshTable
End Sub

Put it in your worksheet code module, see this post for more details.

Back to top

11. Copy a pivot table without

If you want to share your pivot table but not the source data, follow these steps.

  1. Select the pivot table you want to copy
  2. Press Ctrl +C or press copy button on tab "Home" on the ribbon
    unlink pivot table
  3. Press with right mouse button on on a cell where you want to paste the pivot table
    unlink pivot table1
  4. Press with mouse on "Paste Special..."
    unlink pivot table2
  5. Select "Values"
  6. Press with left mouse button on OK button
  7. Repeat step 3 and 4 and then select "Formats"
    unlink pivot table3
  8. Press with left mouse button on OK button

unlink pivot table4

You can tell that the copy has no link to source data by first selecting a single cell in the copied pivot table and then a single cell in the original pivot table. The are two more tabs (Analyze and Design) on the ribbon if the original pivot table is selected

Back to top

12. Customize the layout

You can easily change the pivot table layout with one of the pivot table styles or create a entirely new one.

Select a cell in the pivot table. Go to tab design. Press with mouse on a style and the pivot table is instantly changed. You can hover with mouse pointer over different styles and see the changes to your pivot table change before you make up your mind.

customize pivot table

What happens if I record a macro while changing pivot table style?

Sub Macro2()
ActiveSheet.PivotTables("PivotTable1").TableStyle2 = "PivotStyleLight14"
End Sub

To create a new pivot table style, go to tab Design. Press with left mouse button on the arrow in the lower right corner of pivottable styles window, see picture below.

new pivot table style

Select a table element and press with left mouse button on "Format" button.

pivot table new style

Change formatting and press with left mouse button on OK button.

pivot table new style1

Repeat with the remaining table elements you want to change.

Back to top

13. Consolidate data from multiple cell ranges

Excel has a feature that lets you consolidate data from multiple pivot tables or cell ranges.

Here are my two pivot tables for 2015 and 2014. I want to consolidate both pivot tables into one, notice that they share the same structure

Pivot table 1 - 2015

pivot table - consolidate worksheets

Pivot table 2 - 2014

pivot table - consolidate worksheets1

Here is how to do it.

  1. Press Alt + D + P (This opens the pivot table and pivot chart wizard)
    pivot table - wizard
  2. Press with mouse on "Multiple consolidation ranges"
  3. Press with left mouse button on Next
    pivot table - wizard1
  4. Select "Create a single page field for me"
  5. Press with left mouse button on Next button
  6. Select the first worksheet range you want to consolidate
    pivot table - wizard2
    Notice that I select the row and column labels as well but not the grand totals.
  7. Press with left mouse button on Add button
  8. Repeat step 6 and 7 with your remaining ranges.
  9. Press with left mouse button on Next button
  10. Select if you want to create a new pivot table on an existing sheet or a new worksheet
    pivot table - wizard3
  11. Press with left mouse button on Finish
    pivot table - consolidate worksheets2
  12. Left press with left mouse button on arrow next to Count of Value
    pivot table - consolidate worksheets3
  13. Press with left mouse button on "Value Field Settings..."
    pivot table - consolidate worksheets4
  14. Press with left mouse button on "Sum"
  15. Press with left mouse button on OK

pivot table - consolidate worksheets5

Back to top