--- Bài mới hơn ---
Hướng Dẫn Cách Dùng Hàm Pi Trong Power Bi Dax
The Complete Guide To Power Query
Power Query Overview: An Introduction To Excel’s Most Powerful Data Tool
How To Create A Microsoft Query In Excel (Excel Query)
Hướng Dẫn Cách Sử Dụng Power Query Để Nạp Dữ Liệu Vào Power Pivot
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
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.
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.
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.
2. Open the Sheet Module that contains your source data.
3. Add a new event for worksheet changes.
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:
ThisWorkbook.RefreshAll
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
The refresh puts the event in a recursive loop, and can end up crashing Excel. Here is the code to pvent that.
Application.EnableEvents = False
ThisWorkbook.RefreshAll
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.
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.
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.
The keyboard shortcut to clear all breakpoints is: Ctrl+ Shift+ F9
Refreshing Pivot Tables Without a Macro
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:
- Go to the Analyze tab in the ribbon.
- Choose the Options button.
- Go to the Data tab in the new window that opens.
- Check the box that says, “Refresh data when opening the file.”
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
pc.Refresh
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.
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()
ThisWorkbook.RefreshAll
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 pvent 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… 🙂
--- Bài cũ hơn ---
How To Refresh Pivot Table When Data Changes In Excel?
How To Refresh Pivot Table In Excel (Manually + Auto
Power Pivot For Excel Tutorial: Top Use Cases And Examples
How To Use Powerpivot In Excel: The Ultimate Guide
Hướng Dẫn Cách Phân Biệt Giữa Power Pivot Và Pivot Table