Refresh Pivot Tables Automatically When Source Data Changes

Bottom Line: Learn how to use a simple macro to refresh pivot tables automatically whenever changes are made to the source data.  I also share a non-macro solution to update the pivot tables when the file is opened.  Includes video tutorial and Excel file download.

Skill Level: Intermediate

Watch on YouTube and give it a thumbs up.YouTube Thumbs Up LikeYouTube Subscribe Logo Excel Campus

Download the Excel File

If you learn best by doing it on your own, you can download the file I'm using in the video to follow along.  Here is the Excel file that contains the VBA code.

Refresh Pivot Table Automatically.xlsm (41.5 KB)

Update Pivot Tables Automatically

Can your pivot tables be updated immediately and automatically when their source data changes?

Absolutely.  It requires the use of a really simple macro that I will show you how to create below.

Refresh Pivot Table when Source Data is Changed

If you’re not too familiar with macros and VBA, I suggest checking out my free 3-part video series on getting started with Macros & VBA.

Also, if you are new to pivot tables, I have a series to walk you through what they are and how to use them.  Watch the first video in that series on Pivot Tables & Dashboards

To automatically update our pivot tables, we are going to write a macro with one simple instruction.  That instruction basically says: when I make a change to my worksheet, refresh all the pivot tables and data connections.  Here are the steps to create the macro.

1. Open the Visual Basic Editor.

You can do this by clicking the Visual Basic button on the Developer tab of the ribbon.

Developer Tab in Excel 2016 with Visual Basic Button to Open VB Editor

The keyboard shortcut for opening the Visual Basic editor is Alt+F11.

If you don’t see the Developer tab, you can make it visible using the instructions here.  You only have to do this once, and then the Developer tab will always be visible every time you open Excel in the future.

2. Open the Sheet Module that contains your source data.

In the Project Explorer window of the Visual Basic editor, locate the workbook that you want to change.  Under that workbook are listed the sheets within the workbook.  Select the sheet that contains the source data.  Then double-click on it.

Double-click on the sheet that contains the source data for your pivot table

If you don’t see the Project Explorer window you can enable it from the View menu (keyboard shortcut: Ctrl+R).

3. Add a new event for worksheet changes.

Double-clicking on the sheet opens up the code module for that object.  Within the code module, we want to create an event macro.  To do so, choose Worksheet in the Object drop-down box on the left.

That will add a Worksheet_SelectionChange event to the module, which we don’t actually want, so we will delete it in just a moment.  Before we do, let’s go to the Procedure drop-down menu on the right and choose Change.

Select Change from the procedure drop-down menu

This adds a new event at the top called Worksheet_Change.  Now we will highlight and delete the unnecessary code below it.

Highlight and delete unnecessary portion

The Worksheet_Change event macro will run any time a change is made to cells in that worksheet. We can add VBA code to the Worksheet_Change event to perform actions when the user edits cells.

Note: The SelectionChange event that is added by default will run any time the user selects a cell in the sheet. Since we only want the code to run when the user edits/changes cells , we use the Change event. Checkout my article on VBA Code Modules & How to Run Macros Based on User Events to learn more about the sheet modules and events.

4. Add the VBA code to refresh all pivot tables.

Next, just below the Worksheet_Change line, type in this instruction:


Type Code ThisWorkbook.Refresh.All

The RefreshAll method will refresh all the pivot tables, queries, and data connections in the workbook. This action is the same as if you manually click the Refresh button on the Data tab.

Add this line of code to the Worksheet_Change event will refresh the workbook whenever a change is made to the worksheet that the code is in.

Pivot Table & Source Data on Same Sheet

Aleksandrs asked a great question on the YouTube video comments.  If your pivot table and source data are on the same sheet then you will need to add code to disable events.

The refresh puts the event in a recursive loop, and can end up crashing Excel.  Here is the code to prevent that.

Application.EnableEvents = False
Application.EnableEvents = True

Checking to Ensure Your Macro Is Running

One way to check if the macro is working is to test it.  Make a change to the source data and see if it is reflected in your pivot table.  If your change isn’t easy to spot because you have too much data, or for some other reason, there’s another way to see if your macro is firing.

In the VB editor, you can click on the gray column just to the left of your Worksheet_Change macro.  This will make a red circle appear.  It also highlights that line of code in red.

Place a stop on a line of a macro

This is called a stop or breakpoint.

The keyboard shortcut to toggle a breakpoint on/off is: F9

Now whenever an action occurs that triggers the macro, Excel will jump to the VB Editor and pause the macro so that you can check the code.  In our case, that action is any change being made in the worksheet.

VBA Code Pauses Execution at Breakpoint Worksheet Change Event Check

You can then press F8 to step through each line, or press F5 to run to the end (or next breakpoint).

If you make a change to the worksheet and Excel doesn’t pull you into the VB Editor, you know there is a problem with the macro not running.  If this is the case, it’s likely that you haven’t saved the file as a macro-enabled workbook (.xlsm), and/or enabled macros. You might need to save & close the file, then re-open it and enable macros.

To remove the breakpoint that you’ve placed on the macro, just click on the red circle to make it disappear (keyboard shortcut: F9).

The keyboard shortcut to clear all breakpoints is: Ctrl+Shift+F9

Refreshing Pivot Tables Without a Macro

One disadvantage to using this macro to refresh your pivot tables is that any Undo history is lost each time the macro runs.  In other words, when you click the Undo button (or press Ctrl+Z), Excel doesn’t remember the last thing you did, so it can’t undo it.  Consequently, nothing will happen, and your last change will not be undone.

There is an alternative that allows you to keep your Undo history.  However, this alternative only refreshes your pivot table when the workbook is opened, not every time a change is made.  Here is how you can use that option.

Starting from any cell in your pivot table:

  1. Go to the Analyze tab in the ribbon.
  2. Choose the Options button.
  3. Go to the Data tab in the new window that opens.
  4. Check the box that says, “Refresh data when opening the file.”
Steps to refresh pivot tables when the file is opened

Click to Enlarge

After clicking OK, you might get the get the following warning message if you have multiple pivot tables created from the same source data range.  Just click OK to get through it.

warning messge when setting pivot table to refresh when files is opened

Click to Enlarge

Again, just by way of comparison, if you use this option you retain Undo history, but it only refreshes the pivot table when the workbook is closed and reopened.  If you use the macro option, you lose Undo history, but the pivot table automatically updates whenever any change is made in the workbook.

Variations for Refreshing Pivot Tables

The macro we looked at will not only refresh your pivot tables, but will also refresh any queries as well.  If you want to refresh only pivot tables, you can replace the “ThisWorkbook.RefreshAll” command with this code instead:

Sub Refresh_All_Pivot_Table_Caches()
'Refresh all pivot caches in the workbook.
'Pivot tables are automatically refreshed when cache is refreshed.

Dim pc As PivotCache

  'Refresh all pivot tables
  For Each pc In ThisWorkbook.PivotCaches
  Next pc
End Sub

Each pivot table is connected to an underlying pivot cache, which is connected to the source data. The code loops through all pivot caches in the workbook and refreshes each one. Pivot tables from the same source range can share pivot caches, so this method is faster than looping through all pivot tables.

Similarly, let's say you only want to refresh one particular pivot table.  In that case, you can swap out the “ThisWorkbook.RefreshAll” code with the code below.

Refresh One Particular Pivot Table

And finally, if you are using Power Query and want to disable the background refresh so that queries are refreshed BEFORE pivot tables, I have written an article to explain how to do that by disabling the background refresh on the queries.

Use the Deactivate Event Instead

Another option is to use the Worksheet_Deactivate event instead of Worksheet_Change.  The Worksheet_Deactivate event will run every time the user leaves the sheet and selects a different sheet.  This allows the user to make all the changes to the source data, then the pivot table will be automatically refreshed when they go to any other sheet, including the sheets that contain the pivot table.

Private Sub Worksheet_Deactivate()
End Sub

This code would still be placed in the sheet module that contains the source data.  This is a good option if your pivot tables or data connections take a few seconds or longer to update, and you don't want to wait every time a change is made to the source data.

The only time you might not want to use this is if your pivot table and source data are on the same sheet.  That will usually be a rare case, and something I generally don't recommend.

Thanks to the suggestion from Ted on this one.

Save Time & Embarrassment

I hope this article helps save you time and makes it easier for users of your files.  It can also help prevent embarrassment when you forget to refresh pivot tables before sending out reports.  Believe me, I've made this mistake more times than I'd like to admit… 🙂

Please leave a comment below with questions or suggestions.  Thank you!

Please share
Jon Acampora

Welcome to Excel Campus! I am excited you are here. My name is Jon and my goal is to help you learn Excel to save time with your job and advance in your career. I've been an avid Excel user and VBA developer for 10+ years. I am also a Microsoft MVP. I try to learn something new everyday, and want to share this knowledge with you to help you improve your skills. When I'm not looking at spreadsheets, I get outdoors and surf. :) more about me

Click Here to Leave a Comment Below 48 comments
Victoria - January 4, 2019

Hello –
I have a question about using Microsoft Office 365 ProPlus. I’m trying to get my pivot table in excel to refresh when I add new data to the data set (as you show above.) The pivot table seems only to be refreshing the current selected range. In other words, “Select All” doesn’t work. When I change data in the current select range, it works fine. But adding new data doesn’t update the pivot table. I am having to change the data source (range) manually every time. The table/range seems to be static. I’ve even tried creating a macro to “Select All” but that doesn’t work either. Where am I going wrong?

Sophie - December 14, 2018

Hi Jon, thanks a lot for the great video!

I have a question regarding “RefrehsAll” code. I tried to apply this code and I have faced the following problem:

The code works partially, more like “Worksheet_Deactivate” function you describe above. That said, when I change input data on Sheet-1 (Assumptions sheet), I have to go to the other sheet (Sheet2), where I have Table-1 (linked to Sheet-1) and PivotTable-1 (linked to Table-1) and physically click (just click) any cell of the table to refresh it.

If you have ever experienced it before and can share your experience how this can be fixed, it would be much appreciated! Thanks a lot!


Glenn Johnson - November 28, 2018

Hi Jon – thanks for the good tips! What is the best way to expand the data source for a pivot table. I cut/past new data into a “data” tab regularly. This adds lines of data so I have to manually change the data source range. Just wanted to see if there was an easier way?

Olia - November 2, 2018

Hi, I’m working with paranoid Financials (PF) & oversized Excel files, and we would like to have a button that would refresh all pivots in the workbook and/or tab. this because unless they can press a button, PF don’t believe that anything works.

Also, the auditors are coming, which is raising paranoia to new highs.

David O. - October 12, 2018

Hi Jon!

This couldn’t have come at a better time, as my need for this just came up and it works perfectly!

One “semi-related” question, though. I’m using this macro for a number of pivot tables based on one large table with all the source data which gets updated on a weekly basis. The table consists of five columns of hard coded values which get pasted in each week, as well as a few additional columns with formulas based off the values.

My main concern is that one week may have less rows of data then the previous week. Is there a way to ensure that when any new data is entered, all the old data (aside for the formula columns)is deleted, so that ONLY the new data remains?



    Jon Acampora - October 12, 2018

    Hey Dave,

    Happy to hear it helped. Great question! There are a few ways to go about it. First of all it’s best if your data is in an Excel Table. I assume it is, but here is an article on https://www.excelcampus.com/pivot-tables/table-source-pivot-table/.

    If you are using a macro to copy the data into row 1 of the Table, then you just need to determine how many rows are in the copy range and the Table. You can use variables to store these numbers.

    iCopyRows = rCopyRange.Rows
    iTableRows = lo.ListRows.Count

    You could then use something like the following to delete the extra rows.

    If iTableRows > iCopyRows Then
      lo.DataBodyRange.Rows(iCopyRows + 1 & ":" & iTableRows).Delete
    End If

    I hope that helps. Thanks again and have a nice day! 🙂

Peters Brian - October 1, 2018


Just followed your instructions step by step yet I can’t get the pivot table to update when the source is changed ? The following code is the worksheet where the source data located….any thoughts….does the source have to be a Table ?

Private Sub Worksheet_Change(ByVal Target As Range)


End Sub

Thanks for your help.

    Jon Acampora - October 12, 2018

    Hi Peters,
    You might need to close and re-open the workbook, and make sure macros are enabled when you open it.

    I hope that helps.

terry - September 25, 2018

If several workbooks, each containing pivot tables, are open does “Refresh All” refresh all of the pivot tables or only the ones in the workbook where Refresh All is activated? Thanks

    Jon Acampora - October 12, 2018

    Hi Terry,

    The RefreshAll method only works for the active workbook in this case.

    You could use the following macro to refresh all pivot tables in all open workbooks.

    Sub Refresh_All_PivotTables_in_Workbooks()
    Dim wb As Workbook
      'Loop through all open workbooks and
      'refresh all pivot tables & connections.
      For Each wb In Application.Workbooks
      Next wb
    End Sub

    I hope that helps.

EdH - September 19, 2018

This could be handy for small-ish data sources, or data in your file. We use a lot of larger models via Power Query, so Excel would never know the source changed, and you wouldn’t want it refreshing every time it did anyway.

So I created a query in Power Query that just returns the date and time when it refreshes, then loaded it into the data model, and create the following measure:

Finally I add this formula in Excel to the top of the file:
=”Last Refresh “&TEXT(CUBEVALUE(“ThisWorkbookDataModel”,”[Measures].[LastUpdate]”),”MM/DD/YYYY hh:mm AM/PM”)

Then add a bit of conditional formatting logic so if the refresh is less than X hours old (depends on the data as to how current it needs to be – some are 5minutes, others are 24hrs) it turns Green for good, Red for bad.

    Jon Acampora - October 12, 2018

    Very cool. Thanks for sharing Ed!

RAJESH PATHAK - September 17, 2018

Nice article. All the tips were great, Jon.

sandeep kothari - September 9, 2018

Hi Jon – the Most Valuable Person (MVP) for me! Great article & video.

One question: how do we remove pivot cache (through excel as well as macro, before sending a file containing pivot table to the client?

    Jon Acampora - October 12, 2018

    Hey Sandeep,
    Thank you so much for your kind words and support. Sorry, I just saw this question. I’ll add this to my list for future articles.

    There is an option named “Save source data with file” on the Data tab in the Pivot Table Options menu. You can uncheck that to remove the cache.

    In VBA we can change the SaveData property of the pivot table to do the same.

    ActiveSheet.PivotTables("PivotTable1").SaveData = False

    I hope that helps.

Anterea - September 3, 2018

Hi Jon,

I apply your VBA code “ThisWorkbook.RefreshAll” but still same as manually refresh.
Eg. I enter transaction on my source data and I hope the pivot table automatically refreshed but not. Still need manual refresh.

Look forward to your kind assistance.

kind regards

    Jon Acampora - October 12, 2018

    Hi Antrea,
    You might need to close and re-open the file, and enable macros.

DR.MUHAMMAD SOHAIL - September 2, 2018

Thanks multi millions Jon for sharing your skills with us .
You are the best teacher and no doubt MVP for Microsoft…….you deserve it.

    Jon Acampora - October 12, 2018

    Thank you Dr. Muhammad! I appreciate your support. 🙂

Michael - September 2, 2018

Hi Jon,

Would it help to reference the source table to the target variable in the Worksheet change event macro, so that only when data was added or changed, the macro is running? Any other change on that tab would not do so. How would the reference to the list object look like?

Thanks for your great work!


    Jon Acampora - September 20, 2018

    Hey Michael,
    Great question! We can check to see if the cell that is changed is in the Table (ListObject).

    However, the problem with referencing cells in the ListObject is that the worksheet change might not pick those up if you add new rows or columns. If the change is within the existing table and the structure doesn’t change, then you can use something like the following.

    If Not Intersect(Target, Sheet2.ListObjects(1).DataBodyRange) Is Nothing Then
    End If

    Again, the above will NOT work if you add new rows or columns to the Table.

    The automatic resize on the Table happens AFTER the change event is fired.

    You could reference all possible rows and/or columns that the Table could be in. Something like the following.

    If Not Intersect(Target, Sheet2.Rows("4:10000"), Sheet2.Columns("A:Y")) Is Nothing Then
    End If

    I hope that helps.

Shohel - August 30, 2018


Thanks for all your videos I’m a big fan of you and always learning from you.

I did the macro code as you mentioned and my pivot tables are really slow I mean each time I insert something it takes very long to be able to insert another line item. Even when before this my excel was working very slow and now since it’s updating pivot tables as I enter source data it’s gone more slow.

Please assist if there is something I can do overcome this problem. All this slow motion started when I Inserted two pivot tables from one source data.

    Jon Acampora - October 12, 2018

    Hi Shohel,

    I’m sorry to not reply sooner. I missed your comment.

    If you have a lot of rows of data and/or formulas, then the calculation and refresh could slow down. You might want to try the 64-bit version of Excel.

    In regards to the macro, you can use the WorksheetDeactivate event to only do the refresh when the user is on the source data sheet and then selects another sheet.

    This basically refreshes the pivot tables after you make all changes to the source data, then leave the sheet.

    Private Sub Worksheet_Deactivate()
    End Sub

    The only time this won’t work is if you have pivot tables on the same sheet as the source data. I usually don’t recommend that setup anyways, but just good to know.

    I hope that helps. Thanks again and have a nice day! 🙂

Jang Bahadur Rana - August 29, 2018

Dear Sir, I am not clever on Excel but I am eager to learn Excel and Pivot Table and just join in this comments box. I personally thanking for providing lot and lot of Tips and information about Excel Pivot Table etc and I am following your instruction well.

Once more thank you for your support. I hope it will go longer than my life in future.

Best regards

Jang Bahadur Rana

Tim - August 29, 2018


Can you be clearer if the recommendations, macros, hints, etc apply to Windows only and/or Mac products. As an Apple guy, it’s never clear whether your macros run on Mac or not.

Thanks, Tim

    Jon Acampora - August 29, 2018

    Hi Tim,

    Yes, great point! Thank you for the feedback. We will include that in future posts.

    The macros and techniques in this post should work (have not tested yet) on the Mac versions of Excel. You will need to be on the latest version of Excel 2016 (Office 365) to see the new VB Editor on the Mac that includes the sheet modules and object & procedure drop-downs to create the events.

    I believe this would work on Mac 2011 as well.

    I hope that helps. Thanks again! 🙂

John - August 29, 2018


thank you for the great advice and well articulated explanations. I learn something new from you frequently. Thanks for your generosity, and ability to provide your knowledge and experience.

    Jon Acampora - August 29, 2018

    Thank you for your support John! I’m happy to hear you are learning more about Excel. Awesome! 🙂

Frank - August 29, 2018


is there a reason that you are not using PivotTable.RefreshTable to refresh one particular pivot table?

    Jon Acampora - August 29, 2018

    Hi Frank,

    I believe the results will almost always be the same. I say almost because there might be cases I’m not aware of.

    The RefreshTable method does refresh the pivot cache as well. So if there are multiple pivot tables based on the same source data range (and pivot cache), running the RefreshTable method on one pivot table will refresh all related pivot tables.

    So I think it really comes down to a matter of preference. The macro recorder produces the PivotCache.Refresh code. I use this as it is something that more people have likely seen, or will have questions on.

    When you are refreshing all pivot tables only, and not using the Workbooks.RefreshAll method, then it can be more efficient to just loop through the pivot caches. A source data range could have dozens of pivot tables based on it that all share the same pivot cache. Therefore, looping the pivot cache might mean only doing a few iterations. Looping all the pivot table could be dozens or more. The time savings will only really matter if your pivot tables take a second or more to refresh. That could add up.

    I hope that helps.

Surabhi Veenapani - August 29, 2018

I really appreciate all the excellent tips/tutorials you send out! Hope you keep doing this for a long time!

    Jon Acampora - August 29, 2018

    Thank you Surabhi! I appreciate you support. I have no plans on stopping anytime soon. I love learning and teaching Excel! 🙂

João Pessoa - August 29, 2018

Hi John,

Just one question, if my pivot tables are in another workbook and i don’t want that workbook to be macro-enabled, what code should i write?

    Jon Acampora - August 29, 2018

    Hi Joao,

    Is the source data also in that other workbook that you don’t want to be macro enabled? In other words, is the source data in the same workbook as the pivot tables?

    In that case you could reference the workbook by its name using the Workbooks property.


    You would have to run that macro manually, since the Worksheet_Change event is only going to fire in the workbook that the code & source data is in.

    You can use AppEvents and a class module to potentially monitor other open workbooks for changes to their source data ranges, and refresh automatically. That code is going to be more complex, and something you would want to store in an add-in or your Personal Macro Workbook.

    I hope that helps.

Mickael - August 29, 2018


Thank you for your post.

Why did you use thisworkbook as opposed to activeworkbook.refreshall?
Is there a preference between the two?

    Jon Acampora - August 29, 2018

    Great question Mickael!

    The ThisWorkbook property refers to the workbook that the code is in. Since the code is in the sheet module for the source data range, this will ensure that the workbook that the code is in is always refreshed.

    The ActiveWorkbook property refers to the workbook that the user has active on the computer.

    Most of the time ThisWorkbook and the ActiveWorkbook will be the same when the user is making changes to the source data sheet.

    HOWEVER, there could be cases where you are running a macro to update the source data range. You might be copying data from one workbook into another. In this case the ActiveWorkbook might be different from ThisWorkbook. Even though you copy data into ThisWorkbook, it does NOT necessarily become the ActiveWorkbook.

    If you used ActiveWorkbook.RefreshAll then it could refresh the connections and pivot tables in the wrong workbook. Therefore, it’s best to use ThisWorkbook in this case. You might not be using macros to update the file now, but this will help future proof the code.

    I hope that helps. Thanks again for the question! 🙂

Resit O - August 29, 2018

In order to get to the “Data” tab, you can simply right click on the pivot table and choose “PivotTable Options”.

And then check the box that says, “Refresh data when opening the file.”

    Jon Acampora - August 29, 2018

    Thank you for the suggestion Resit! We will update the article with that method as well. Definitely a little faster! 🙂

Parhat - August 29, 2018

Hello Jon,

Excellent tips, very useful information for our daily work.

Keep up the good job.


John - August 29, 2018

Hi Jon, thanks. This is great to know. I have a question: I have pivot tables based on SQL data that imports every time I open the file. Will the macro that says ThisWorkbook.Refreshall refresh before or after the import of SQL data? Thanks again, John.

Anne Lupkoski - August 29, 2018

Jon, you write the BEST instructions! Appreciate the coloured arrows and text boxes that outline key info. Thank you for this very valuable information and for laying it out in such an easy-to-understand/follow format.

    Jon Acampora - August 29, 2018

    Thank you Anne! I really appreciate the nice feedback, and happy to hear the images are useful. I am a visual learner too, and images really help me learn and retain the info. 🙂

Cristian - August 29, 2018

useful, as always 🙂 thanks Jon for sharing your knowledge and experience with us.

Anne Mette Kristensen - August 29, 2018

You probably already know – the shortcut for updating a pivot table is ALT+F5!!! I absolutely love that new shortcut 😉 Hope someone else might be able to learn and use 🙂

    Jon Acampora - August 29, 2018

    Thanks so much Anne! We will add this to the article. The keyboard shortcut to Refresh All pivot tables and connections is Ctrl+Alt+F5. That’s one I use all the time as well. 🙂


Leave a Reply: