Table of Contents
Updating a pivot table is a pain, isn’t it? I’m sure that pivot tables are everyday part of you job for reporting and analyzing the data thus, you will understand it.
The point is every time when you add new data to the source sheet you need to update the source range for the pivot table before you refresh your pivot table. Now just imagine if you add data to your source sheet every day you have to update the source range every day.
And every time changing pivot table range is a mess. Yes, that’s right, the more frequently you add data, the more you need to update the source range.
So the point is you need a method to update the source range automatically when you add new data.
1. Apply Table to have a Auto-Updating Pivot Table Range
A few days back I asked John Michaloudis about his million-dollar pivot table advice. He says:
Believe me, it’s million-dollar advice. By applying a table in source data you don’t need to change the source range of your pivot table again and again. Whenever you add new data, it will automatically update the pivot table range.
Convert Data into a Table Before Creating a Pivot Table
Every time before creating a pivot table make sure to apply the table to source data by using the following steps.
- Select any of the cells in your data.
- Use shortcut key Control + T or Go to → Insert Tab → Tables → Table.
- You will get a pop-up window with your current data range.
- Click OK.
- Now to create a pivot table select any cell of your data. Go to → Design Tab → Tools → Summarize With Pivot Table.
- Click OK.
Now, whenever you add new data into you datasheet it will automatically update pivot table range and you just have to refresh your pivot table.
Convert Data into a Table After Creating a Pivot Table
If you already have a pivot table in your worksheet you can use following steps to convert your data source into a table.
- Select any of the cells in your data source.
- Use shortcut key Control + T or Go to → Insert Tab → Tables → Table.
- You will get a pop-up window with your current data range.
- Click OK.
- Now, select any of cells from your pivot table and Go to → Analyze → Data → Change Data Source → Change Data Source (Drop Down Menu).
- You will get a pop-up window to re-select your data source or you can also enter the name of the table into the range input.
- Click OK.
From now on-wards every time when you add new data into your source sheet it will increase pivot table range to automatically update it.
2. Create a Dynamic Pivot Table Range with OFFSET Function
The other best way to update the pivot table range automatically is to use a dynamic range. Dynamic range can expand automatically whenever you add new data into your source sheet. Following are the steps to create a dynamic range.
- Go to → Formulas Tab → Defined Names → Name Manager.
- Once you click on name manager you will get a pop-up window.
- In your name manager window click on new to create a named range.
- In your new name window, enter
- A name for your new range. I am using the name “SourceData”.
- Specify the scope of the range. You can specify between the current worksheet or workbook.
- Add a comment to describe your named range. Enter the below formula to “Refer to” input bar.
=OFFSET(PivotTableData!$A$1,0,0,COUNTA(PivotTableData!$A:$A),COUNTA(PivotTableData!$1:$1))
- In the end, click OK.
Now, you have a dynamic range to create a pivot table. All you have to do is just create a pivot table with your source data and after that change the source with the named range using the same method which I have used in the first method of tables. Once you add new data to your source sheet, just refresh your pivot table.
How does this Formula Works?
In the above formula, I have used the offset function to create a dynamic range. I have mentioned cell A1 as the starting point and then without mentioning rows and columns, I have specified the height and width of the range by using COUNTA.
COUNTA will count the cells with values from column A and row 1 and tell offset to expand its height and width accordingly. The only thing you have to take care that there should be no blank cell in between column A and row 1.
3. Update Pivot Table using a VBA Code
Most people love to use VBA codes. So here is the code to use to update the pivot table range with VBA.
Sub UpdatePivotTableRange()
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("PivotTableData3")
Set Pivot_Sheet = ThisWorkbook.Worksheets("Pivot3")
'Enter in Pivot Table Name
PivotName = "PivotTable2"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
'Change Pivot Table Data Source Range Address
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
'Ensure Pivot Table is Refreshed
Pivot_Sheet.PivotTables(PivotName).RefreshTable
'Complete Message
Pivot_Sheet.Activate
MsgBox "Your Pivot Table is now updated."
End Sub
Things you have to change before you use it in your workbook.
- Line13: Change the name of source worksheet.
- Line14: Change the name of pivot table sheet.
- Line17: Change the name of the pivot table.
If you still have a problem to use this code, please write me in the comment box. Now, let me show you how this code works so that you can easily modify it as per your need.
Set Data_Sheet = ThisWorkbook.Worksheets("PivotTableData3")
Set Pivot_Sheet = ThisWorkbook.Worksheets("Pivot3")
In the above part of the code, we have specified the variables for the pivot table and source data worksheet. You can change the name of the worksheet from here.
PivotName = "PivotTable2"
In above part of the code, enter the name of the pivot table on which you want to use this code.
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
Above part of the code will create a dynamic range by using cell A1 from the data source worksheet. It will check the last column and last row with data to create a dynamic range. Every time you run this macro it will create a new dynamic range.
'Ensure Pivot Table is Refreshed
Pivot_Sheet.PivotTables(PivotName).RefreshTable
'Complete Message
Pivot_Sheet.Activate
MsgBox "Your Pivot Table is now updated."
Above part of the code will refresh the pivot table and show a message to the user that pivot table in updated now.