Refreshing Pivot Tables

I have a certain workbook where I keep some statistics. I've created a small PivotTable, with a PivotChart attached to it. I like to have the chart update in real time, so I use a bit of code to do that. The following code goes in the sheet module for the particular sheet I want to update.

Private Sub Worksheet_Change(ByVal Target As Range)
' runs whenever anything on the worksheet is changed

With Application
  .ScreenUpdating = False
  .EnableEvents = False
End With

Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
  pt.RefreshTable
Next pt

With Application
  .ScreenUpdating = True
  .EnableEvents = True
End With

End Sub

Certain weirdness starts happening with the sheet, however. For example, if you copy a range, the marching ants around the cell(s) disappears right after the first paste, so you can't copy and then paste repeatedly until you press Escape.

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button
Comments on this article are closed. Why?

Site last updated: February 9, 2012