Build a pivot cache from the data, then create the pivot table from the cache and set its fields. This version reads a sheet named Data with headings in row 1:
Sub CreatePivot()
Dim wsData As Worksheet, wsPT As Worksheet
Dim pc As PivotCache, pt As PivotTable
Set wsData = ThisWorkbook.Worksheets("Data")
Set wsPT = ThisWorkbook.Worksheets.Add(After:=wsData)
Set pc = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=wsData.Range("A1").CurrentRegion.Address(External:=True))
Set pt = pc.CreatePivotTable( _
TableDestination:=wsPT.Range("A3"), TableName:="SalesPivot")
With pt
.PivotFields("Region").Orientation = xlRowField
.PivotFields("Product").Orientation = xlColumnField
.AddDataField .PivotFields("Sales"), "Total Sales", xlSum
End With
End Sub
Change the sheet name and the field names ("Region", "Product", "Sales") to match your headings exactly. A field name that doesn’t exist stops the macro with error 1004, and so does a data field caption that matches a heading. That’s why the sum is captioned “Total Sales” rather than “Sales”.
If the source data is an Excel Table, use SourceData:="Table1" so new rows are included whenever the pivot refreshes. To refresh an existing pivot instead of rebuilding it, use wsPT.PivotTables("SalesPivot").PivotCache.Refresh.
Avoid wrapping the macro in On Error Resume Next. It hides the real error, so you end up with a new sheet and no pivot table. To put a field in the filter area, add .PivotFields("Region").Orientation = xlPageField.
Pivot Tables are one of the Intermediate Excel Skills, and this is an Advanced Pivot Table Tutorial that helps you to master pivot tables.
The fact is that when it comes to data analysis, quick and effective reporting, or presenting summarized data, nothing can beat a pivot table.
It is dynamic and flexible. Even if you compare formulas and pivot tables, you will find that pivot tables are easy to use and manage. If you want to take your pivot table skills to the next level, the best way is to have a list of tips and tricks that you can learn.
In this tutorial, I’ve used the words “Analyze Tab” and “Design Tab”. To get both of these tabs on the Excel ribbon, you need to select a pivot table first. In Microsoft 365 and the latest versions of Excel, the Analyze tab is called “PivotTable Analyze” (in Excel 2010, it was the “Options” tab). Apart from this, make sure to download this sample file from here.
Tips and Tricks to Help You Become a Pivot Table PRO
Using Pivot Charts with Pivot Tables to Visualize Your Report
I’m a big fan of the pivot chart. If you know how to use a pivot chart properly, you can make the best out of one of the best Excel tools.
- Select a cell from the pivot table and go to “Analyze tab”.
- In the “Analyze Tab”, click on “PivotChart” (in the Tools group), select the chart type, and click OK.

It will instantly create a pivot chart from the pivot table you have. And when you insert a new pivot chart, it comes with some buttons to filter it, which sometimes are not really useful. If you think so, you can hide all of them or some of them.

Right-click on the button and select the “Hide … Field Buttons on Chart” option for that button (like “Hide Value Field Buttons on Chart”) to hide it, or click on “Hide All Field Buttons on Chart” to hide all the buttons.

When you hide all the buttons from a pivot chart, it also hides the filter button from the bottom of the chart but you can still filter it using the pivot table filter, slicer, or a timeline.
You can also insert a pivot chart from the Insert tab: select a cell in the pivot table and go to Insert ➜ PivotChart. And to hide or show all the field buttons from the ribbon, select the chart and go to PivotChart Analyze ➜ Show/Hide ➜ Field Buttons.
Pivot Table Keyboard Shortcuts
Once you start using pivot tables every day, these keyboard shortcuts (Excel for Windows) can save you a lot of clicks:
- Alt + N + V + T: Insert a pivot table from the selected data (Insert ➜ PivotTable ➜ From Table/Range).
- Alt + D + P: Open the classic PivotTable and PivotChart Wizard.
- Alt + F5: Refresh the selected pivot table.
- Ctrl + Alt + F5: Refresh all the pivot tables and data connections in the workbook (Refresh All).
- Alt + Shift + Right Arrow: Group the selected items.
- Alt + Shift + Left Arrow: Ungroup the selected items.
- Ctrl + Minus (-): Hide the selected item.
- Alt + F1 / F11: Insert a pivot chart on the same sheet / on a new chart sheet.
On a Mac, the Alt key sequences don’t work, and the classic wizard isn’t there. You can use the same options from the ribbon or the right-click menu instead.
Create a Pivot Table with a Formula (PIVOTBY and GROUPBY)
In Excel for Microsoft 365 (and Excel for the web), there are two newer functions that create a pivot-style summary with a formula. The best thing is that the result updates automatically when the source data changes, so there’s no need to refresh it.
Let’s say you have regions in A2:A100, products in B2:B100, and amounts in D2:D100. GROUPBY gives you one row for each region with the total amount, just like a pivot table with one row field:
=GROUPBY(A2:A100,D2:D100,SUM)
And PIVOTBY adds a column field as well, so you get regions in rows, products in columns, and the total amounts in between:
=PIVOTBY(A2:A100,B2:B100,D2:D100,SUM)
Both functions add totals by default, and you can replace SUM with AVERAGE, COUNT, MAX, or MIN. If you are using Excel 2021 or an older version, you can get the same total for one region with SUMIFS (here, the region name is in F2):
=SUMIFS($D$2:$D$100,$A$2:$A$100,F2)
A pivot table is still the better choice when you want slicers, timelines, pivot charts, or drag-and-drop changes. Use PIVOTBY or GROUPBY when you want a summary that is always up to date and can be used in other formulas.
Fix Common Pivot Table Problems
Below are the problems you are most likely to face while working with a pivot table, and how to fix each one:
- “The PivotTable field name is not valid”: One of the columns in the source data has no header, or the range you selected doesn’t start with the header row. Give every column a unique header and select the data again.
- “A PivotTable report cannot overlap another PivotTable report”: Two pivot tables are too close on the same sheet, and when one of them grows on refresh, it runs into the other. Move one of them to a new worksheet or leave more space between them.
- New data doesn’t show up: Refresh the pivot table first. If the new rows are below the source range, change the data source (PivotTable Analyze ➜ Change Data Source), or convert the source data into a table so that it expands automatically.
- Count instead of Sum: The value column has blank cells or numbers stored as text. Fix the source data, or open the Value Field Settings and select Sum.
- “Cannot group that selection”: The date column has text values or blank cells. Convert the text dates into real dates and remove the blanks, and then refresh the pivot table before you group again.
Pivot Tables on Mac, Excel for the Web, and Google Sheets
- Excel for Mac: You get the same PivotTable Analyze and Design tabs, slicers, and timelines, but there is no classic wizard, and a few options (like some data model features) are limited.
- Excel for the web: You can now create and edit pivot tables from Insert ➜ PivotTable, and most of the everyday options are there. Some advanced options still need the desktop app.
- Google Sheets: Go to Insert ➜ Pivot table. It works in a similar way, but options like slicers, timelines, and the pivot table styles are different from Excel.
In the End
As I said, pivot tables are one of those tools that can help you get better at creating reports and analyzing data in no time.
And with these tips and tricks, you can save even more time. If you ask me, I want you to start using at least 10 tips first, then go for the next 10, and so on.
But you need to tell me one thing now: What’s your favorite pivot table tip?
Frequently Asked Questions
How do I refresh all the pivot tables in a workbook at once?
Go to Data ➜ Refresh All, or use the keyboard shortcut Ctrl + Alt + F5. You can also go to PivotTable Analyze ➜ Refresh ➜ Refresh All. It refreshes every pivot table and data connection in the workbook in one go.
Why does my pivot table show Count instead of Sum?
Excel uses Sum only when all the cells in the value column are numbers. If there is a blank cell or a number stored as text, it uses Count. Right-click on the value field, go to Summarize Values By, and select Sum, or fix the source data and refresh.
Why can’t I see the PivotTable Analyze tab?
The PivotTable Analyze and Design tabs only show up when you select a cell inside a pivot table. Click anywhere in the pivot table, and both tabs will appear on the ribbon.
Does a pivot table update automatically?
No, a pivot table only updates when you refresh it. You can set it to refresh when the file opens (tip 38), or use the PIVOTBY function in Microsoft 365, which recalculates on its own like any other formula.
Don’t Miss to Read These
- Add-Remove Grand Total in a Pivot Table
- Add Running Total in a Pivot Table
- Automatically Update a Pivot Table
- Formulas in a Pivot Table
- Change Data Source for Pivot Table
- Count Unique Values in a Pivot Table
- Delete a Pivot Table
- Filter a Pivot Table
- Add Ranks in Pivot Table
- Conditional Formatting to a Pivot Table
Super sir tq
Hi dear friends , I appreciate your hard work in creating these useful information and hope you all the best in life and your business .
Hi Puneet, sometimes I hit the keyboard by accident when the focus is on an empty cell, and another empty cell is placed inside the cell, I need to know how to delete the inserted cell without having to copy everything into a new sheet except for the corrupted cell… any help appreciated, this has happened to me so many times.
Thank you very much, a real treasure for me
Have a nice day
Always thankful to you for the excellent tutorials and tips!
You are welcome.