Locked lesson.
About this lesson
Automating PivotTable refresh operations.
Lesson versions
Multiple versions of this lesson are available, choose the appropriate version for you:
2016, 2019/365.
Exercise files
Download the ‘before’ and ‘after’ Excel workbooks from the video tutorial and try the lesson yourself.
Automating Refresh.xlsx272.9 KB Automating Refresh - Completed.xlsm
279.6 KB
Quick reference
Automating Refresh
Automating PivotTable refresh operations.
Where/when to use the technique
You want to automatically refresh your PivotTable data to make sure it’s always current.
Instructions
Refresh at file open
- Right-click the PivotTable > PivotTable Options > Data > Refresh data when opening the file
- Note that this must be done for each data source
- This works well for PivotTables connected to databases
Method 1: Refresh each time the PivotTable’s worksheet is selected
- Go to the Developer tab > Visual Basic > Expand the VBAProject > Microsoft Excel Objects
- Double-click the worksheet that holds your pivot and paste in the following code:
Private Sub Worksheet_Activate()
ActiveSheet.PivotTables("pvtSales").PivotCache.Refresh
End Sub
- Update pvtSales to the name of your PivotTable
- Close the editor
- This can be done for each sheet you’d like to refresh automatically
Method 2: Refresh every pivot when any sheet is selected
- Go to the Developer tab > Visual Basic > Expand the VBAProject > Microsoft Excel Objects
- Double-click the ThisWorkbook object paste in the following code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ActiveWorkbook.RefreshAll
End Sub
- Close the editor
Key points to remember
- You will get macro prompts on your file from this point forward
- Clicking your pivot table will start a refresh (can be an issue if data sets are huge)
- Use either Method 1 or Method 2, NEVER both
- There may only be one Worksheet_Activate event in any module (If you already have one, you may need help adding this code. Find a forum and ask!)
- There may only be one Workbook_SheetActivate event in the ThisWorkbook module (If you already have one, you may need help adding this code. Find a forum and ask!)
Lesson notes are only available for subscribers.