How to automatically refresh pivot chart in Excel 2007 every time a cell in the source table changes

I have a pivot chart that is built from a table in an excel worksheet.   Whenever I add a row or change an existing row to this table, I want for the pivot chart to automatically update.  Currently I have to hit ctrl-alt-F5 to refresh the data.  Is there a good way to do this in Excel 2007?

Thanks,
Tom
Tom__TomAsked:
Who is Participating?
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Or,  better this code, the first one might get you into an endless loop.

cheers, teylyn
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
    Me.PivotTables(1).PivotCache.Refresh
Application.EnableEvents = True
End Sub

Open in new window

0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello Tom

Right click the sheet tab of the worksheet that contains your data and the pivot table and select View Code. This will open the VBA editor. Copy the code below into the codewindow


Private Sub Worksheet_Change(ByVal Target As Range)
     Me.PivotTables(1).RefreshTable
End Sub

This assumes that there is only one pivot table on the worksheet. If there is more than one pivot table on the sheet the code needs to be adapted.

cheers, teylyn
0
 
DonkeyOteCommented:
teylyn, whenever you toggle events add an Error Handler ... in this instance an On Error Resume Next should suffice.
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
thx, DO, I'll keep that in mind!
0
 
Patrick MatthewsCommented:
Tom,

teylyn seems to have you all set, so no points for this please :)

I always recommend that PivotTables and PivotCharts be on different sheets than the source data.  I then use event code on the sheets holding the PT and PC--that way, if you make multiple changes in the source data, the update only occurs once, when you activate the sheet with the PT or PC.

Please see below.

Patrick



' This is for the worksheet with the PT

Private Sub Worksheet_Activate()
    
    Me.PivotTables(1).RefreshTable
    
End Sub

' This is for the Chart sheet with the PC

Private Sub Chart_Activate()
    
    Worksheets("PT").PivotTables(1).RefreshTable
    
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.