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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.