Link to home
Start Free TrialLog in
Avatar of Tom__Tom
Tom__Tom

asked on

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
Avatar of Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Flag of New Zealand image

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
ASKER CERTIFIED SOLUTION
Avatar of Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DonkeyOte
DonkeyOte

teylyn, whenever you toggle events add an Error Handler ... in this instance an On Error Resume Next should suffice.
thx, DO, I'll keep that in mind!
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