We help IT Professionals succeed at work.

"With Events" class to capture "After Pivot Table Refresh" event

Davisron867
Davisron867 asked
on
Experts, Masters and Sages: Is there a way to create an After Pivot Table Refresh event (not Query Refresh) to fire additional code afterwards?

I have multiple users working in local Read Only copies of an Excel file with Pivot Tables sourcing an Access table. Users refresh their Pivot Tables using the Excel Menu or Context Menu.

After the Pivot refresh, I'll fire additional code to update a "Last Refresh Timestamp" that I compare to the "Data Source Timestamp". When Data Source is updated, the Data Source Timestamp > Last Refresh Timestamp and causes formula "=If(Last refresh > Data Source, "", "Update Available")" to display "Update Available".

When users subsequently Refresh, I want the code to update the last refresh Timestamp and the formula then returns a 0-length string and the notification goes away.

I did ask the question recently but accepted an incorrect answer, ID: 26237589
Comment
Watch Question

You could add a new macro - fired by a button in the sheet, to update the pivot and then refresh the date/timestamp

Steve
Top Expert 2010

Commented:
Hello Davisron867,

Each worksheet has a PivotTableUpdate event, and there is a workbook-level SheetPivotTableUpdate event.

Regards,

Patrick

Author

Commented:
For the SheetPivotTableUpdate event to work, it appears you need to EnableEvents.
Private Sub Workbook_Open()
Application.EnableEvents = True
End Sub

And then the following executes once for each pivot table in the workbook.

Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
With Sheet6.Range("B7")
    .FormulaR1C1 = "=[MGMTREP_LEDGER_BAL.xlsm]Parameters!R7C2"
    .NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"
End With
Sheet6.Cells(7, 3) = Now()
End Sub
Top Expert 2010
Commented:
Davisron867 said:
>>For the SheetPivotTableUpdate event to work, it appears you need to EnableEvents.

Well, for any *event* code to work, events must be enabled.  By default, events are enabled, so unless you are
turning them off elsewhere, that's no problem.

>>And then the following executes once for each pivot table in the workbook.

It fires whenever a PT updates in that workbook.  You could have 10 PTs in a workbook, but if only one updates,
the event fires just once.

Author

Commented:
negative...I have 5 pivot tables and it runs 5 times in succession, moving from one to the next as if in a loop.
Top Expert 2010

Commented:
Davisron867,

If your PTs are based off of other PTs, when the "parent" updates so will the children.

Also, if your PTs are set to refresh on open, when you open the file, they will refresh.

Do you have other code that is forcing refreshes?

Patrick

Author

Commented:
In fact I was turning off events somewhere.