How to Automatically Update a Pivot Table Range in Excel

Updating a pivot table is a pain…

…isn't it?

If you use pivot tables in your work frequently...

...I’m sure you can understand.

The point is:

Every time when you add new data in 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 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 source range automatically...

...when you add new data.

Top 3 Ways for Updating a Pivot Table Range Automatically

In this post, I’d like to share with you 3 different methods...

...which you can use to update the source range of a pivot table automatically.

  1. Apply Table
  2. OFFSET Function
  3. VBA Code

...so without any further ado, let's get started.

1. Apply Table to have a Auto Updating Pivot Table Range

A few days back I asked with John Michaloudis about his million dollar pivot table advice.

He says:

Put your source data in a table.

Believe me, it's a 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 pivot table range.

Convert Data into a Table Before Creating a Pivot Table

add table to update pivot table range
  • Click OK.
  • Now to create a pivot table select any cell of your data. Go to → Design Tab → Tools → Summarize With Pivot Table
new pivot table to update pivot table range
  • 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.
change source data to update pivot table range
  • 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 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.
use name manager to update pivot table range
  • 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 a name “SourceData”.
    • Specify the scope of the range. You can specify between current worksheet or workbook.
    • Add a comment to describe your named range.
      Enter 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.
create a new name to update pivot table range

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 in your source sheet, just refresh your pivot table.

How does this Formula Works?

3. Update Pivot Table using a VBA Code

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.

  1. Line13: Change the name of source worksheet.
  2. Line14: Change the name of pivot table sheet.
  3. 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.

Sample File

Conclusion

Using a dynamic method to update pivot table range can save you a lot of time. You don’t have to change source data again and again.

Just choose one of the above-mentioned methods, put your data on your data sheet and refresh your pivot table.

If you ask me I always use tables.

Now, tell me one thing.

Which method do you really like? VBA or table?

Share your views with me in the comment section, I’d love to hear from you. And, please don’t forget to share this tips with your friends.


About the Author

Puneet Gogia

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.

LinkedIn - YouTube

31 thoughts

Leave a Comment

Your email address will not be published.

  1. Puneet, the VBA code works great, thanks! One Question, what changes should I make to the code so that a second or third pivot table that’s linked to the same data source is also updated/refreshed?

    Thanks in advance,
    Nick

  2. This code is giving me a run-time error. it says subscript out of range.

    Range(“A6”).Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    “PC IT!R6C1:R1575C15″, Version:=xlPivotTableVersion10).CreatePivotTable _
    TableDestination:=”Sheet1!R3C1″, TableName:=”PivotTable1”, DefaultVersion _
    :=xlPivotTableVersion10
    Sheets(“Sheet1”).Select
    Cells(3, 1).Select

    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
    Sheets(“PC IT”).Select
    ‘Set Pivot Table & Source Worksheet

    Set Data_Sheet = ThisWorkbook.Worksheets(“PC IT”)
    Set Pivot_Sheet = ThisWorkbook.Worksheets(“sheet1”)

    ‘Enter in Pivot Table Name
    PivotName = “PivotTable1”
    ‘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.”

  3. I like the table method but already Iam using the same. Thanks for the showing other options too.

  4. Hello!

    I’m trying to implement your VBA code into my workbook but there is a problem.
    After changing the names of the object in line 13,14 and 17 with

    ‘Set Pivot Table & Source Worksheet
    Set Data_Sheet = ThisWorkbook.Worksheets(“SourceTab4”)
    Set Pivot_Sheet = ThisWorkbook.Worksheets(“Pivot4”)

    ‘Enter in Pivot Table Name
    PivotName = “Something”

    when I launch the macro, there is an error window that pop up:

    Method ‘PivotTables’ of object’_Worksheet’ failed:

    And in the debugger this is selected in yellow:
    Pivot_Sheet.PivotTables(PivotName). _
    ChangePivotCache ActiveWorkbook. _
    PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)

    I don’t know if the problem comes from the Pivot Table Name or not because I gave nowhere a name of the pivot table, so I put a random name “Something” into the parenthesis.

    The names of my sheets are : SourceTab4 and Pivot4

    Thanks in advance!

  5. hi bro iam srinivasan iam an commerce student …. i have no knowlege on about vba code in excel kindly provide me some guidance on learning vba in excel…
    my email id [email protected]

  6. I have a pivot table based on a table which has dates in Col B (which is imported by Query from a file). When I insert a slicer choosing the date field it shows dates way beyond my data. Although the dates are not highlighted because my data doesn’t go that far, it bothers me that the scroll bar is active for unused dates. How can I fix this?

    • Hi Willard,

      Try setting your retained data in the Pivottable to ‘None’:

      In your PivotTable Options, click the Data tab and then select ‘None’ from the Number of items to retain per field drop-down list.
      After this refresh your PivotTable.

  7. Hi, I have multiple pivots in my workbook. Not all of it use the same data source. However, most of them use the same data source. Can a macro code me written to refresh all these pivots using the same data source? (Note: The source data also keeps changing (increasing / decreasing no. of rows) that should also be factored in while writing the code

  8. Hi,
    I am trying via vbscript :

    ‘———————————————————————————–
    ‘Set Pivot Table & Source Worksheet
    ‘———————————————————————————–
    Set objExcel = CreateObject(“Excel.Application”)
    Set Data_Sheet = objExcel.Workbooks.Open(“D:\Users\703220609\Documents\rtest\test\test_pvt.xlsx”)
    Set Pivot_Sheet = Data_Sheet.Worksheets(“Sheet2”)

    ‘———————————————————————————–
    ‘Enter in Pivot Table Name
    ‘———————————————————————————–
    PivotName = “PivotTable1”

    ‘———————————————————————————–
    ‘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.”

    I am calling the VBscript via R.
    The error I am getting :
    D:\Users\703220609\Documents\R_Code\script.vbs(21, 69) Microsoft VBScript compilation error: Expected ‘)’

    Please help

  9. In the beginning of your code, you wrote:
    Dim lastRow As Long

    But what is it used for? I can only see it once in the code, and that’s where you named it.

  10. my source data is constantly being deleted / rewritten. so I still haven’t found one of these to work. The second one, SourceData… the offset formula is destroyed by the data-import/delete-empty-rows function i have (it pulls 50+ worksheets from external sources, pastes them in to 50+ sheets then concatenates those 50+ sheets into one master sheet… then removes blank rows). so my source data is constantly changing drastically and none of these methods is working for me yet

  11. Looked for a lot of solutions for refreshing the data source for pivots.
    A lot of complicated solutions… This one, really simple, works perfectly! Thank you a lot!!

  12. I used option 1B but if I copy paste smaller range of data in the sheet, it still keeps the old data. If I clear out the data before copying it, it still keep the Table1 as large as old one. Which means I get “blanks” in pivot table.

  13. I’m getting a Runtime Error 1004 When I get to the Set DataRange Line under ‘Define Start Point & Dynamic Range of Data….Any suggestions?

  14. Don’t use OFFSET! It is volatile, use INDEX instead:
    =$A$2:INDEX($1:$1048576,COUNTA($A:$A),COUNTA($1:$1))
    $A$2 is the upper, left corner of your range.
    You can play around with the INDEX range and the two COUNTA ranges to adjust to your needs.

  15. Thanks for sharing this. However, I get a syntax error message when trying to run and it points to this particular line.

    NewRange = Data_Sheet.Name & “!” & DataRange.Address(ReferenceStyle:=xlR1C1)

    Any idea why/how to fix?

  16. The Table approach works perfectly when we do this manually.. but say we have a template excel file and we have one data sheet and one pivot sheet.. and each time we delete all the rows (except the header one) from the template excel file and insert rows to this data sheet programmatically (with apache POI).. The table approach is NOT working when the number of rows for current iteration is greater than the number of rows in the template file..

  17. When I run the code I get a Run-time error ‘1004’: Method “PivotTables” of object’_Worksheet’ failed. Upon debugging it is pointing the code in the Change Pivot Table Data Source Range Address. Any suggestions?

    • Refer this:

      Line13: Change the name of source worksheet.
      Line14: Change the name of pivot table sheet.
      Line17: Change the name of the pivot table.

  18. Thank you for the above code. It works 100%. But how can I change the above code to only use the actual UsedRange? As I have “” and hidden formulas lower down that I don’t want part of the UsedRange. I don’t want to use xlDown but rather the actual UsedRange….Any Suggestions please? 🙂

    • best thing is to create a table (well, a table is a solution in itself) then use that table name in the source name.