Feb 09 2017
06:40 AM
- last edited on
Jul 25 2018
09:33 AM
by
TechCommunityAP
Feb 09 2017
06:40 AM
- last edited on
Jul 25 2018
09:33 AM
by
TechCommunityAP
I have a large data query file with a few sheets containing pivot tables. I need to add a button to the first sheet that will refresh all the pivot tables in the file.
Feb 09 2017 06:52 AM
Hello Pierre
Excel already has such a button on the ribbon:
Data -> Connections -> Refresh All
You could also put it in the Quick Access Toolbar if you don't want to switch tabs.
Feb 10 2017 04:23 AM - edited Feb 10 2017 04:24 AM
Dear Pierre,
First and foremost, add the (Developer Tab) to your Excel Ribbon, and follow these steps:
- Go to Insert at the Developer Tab, and click on it.
- You will find two sections: Forms Controls and ActiveX Control, in the ActiveX Control Section, select the command button, and add it to your sheet.
- Double click on the button, to open the VBA Editor, but before that, make sure that the design mode is turned on.
- Copy this command, and paste it in the VBA editor:
ActiveSheet.PivotTables("Your Pivot Table Name").PivotCache.Refresh |
So it would look as follows:
- Add your pivot table name to the code, and save it.
- Close VBA Editor
- Turn Design Mode off
- You have to save your workbook in one of these formats: (XLSM, XLSB, XLS), in order to save Refresh code.
Enjoy :)
Feb 10 2017 01:44 PM
HI Pierre
If you do want to go down the Macro route rather than using the Refresh All button then I'd suggest adding this code to a module.
Best to avoid any hard coded referencing to Pivot Table names or sheets etc.
Sub RefreshAllPivots()
Dim pc As PivotCache
For Each pc In ThisWorkbook.PivotCaches
pc.Refresh
Next pc
End Sub
Feb 10 2017 01:46 PM
Also, when you say Data Query file are you using Power Query and do you want the queries to refresh also, in which case my PivotCache suggestion won't do it for you
This instead....
Sub RefreshAll
ThisWorkbook.RefreshAll
EndSub
Jul 06 2017 12:05 AM
Hi there!
I think I have similar concern here.
I have several excel files with pivot tables each and with at least two sheets and I want to automatically refresh in one click of a button all these files which are all connected to db server.
Would really appreciate your responses.
Thanks!
Jul 06 2017 11:34 PM