Use VBA to refresh pivot tables - excel

I am working on a project, where I dump data on a monthly basis in Sheet1. In my sheet analysis, I have three pivot tables summarizing this data using certain filter, which should remain unchanged even if the raw data changes.
I use the following VBA code to update the pivot tables after the data dump.
Private Sub CommandButton1_Click()
With ThisWorkbook.Worksheets("Analysis")
.PivotTables("PivotTable1").RefreshTable
.PivotTables("PivotTable2").RefreshTable
End With
Sheets("Output").Activate
End Sub
The problem is, whenever I have a new data dump in Sheet 1 and then run the code, I get the following message: There is already data in [Dokument1]Analysis. Do you want to replace it? After this the output from the pivot tables is wrong and the supposedly static pivot filters are no longer there.
I made sure that there is more than enough space between the pivot tables and there cannot be any overlaps. I have had this problem for a long time and do not know how to solve it, so any help is appreciated.

Do not use ActiveSheet. In my opinion is better to use create a with statement refers to the sheet you want and use refresh all instead of one by one.
Option Explicit
Sub test()
With ThisWorkbook.Worksheets("Sheet1")
.RefreshAll
End With
End Sub

This is a nice way to loop through all worksheets and refresh all pivot tables:
Dim ws As Worksheet
Dim pt As PivotTable
For Each ws In wb.Sheets
For Each pt In ws.PivotTables
pt.PivotCache.Refresh
Next pt
Next ws
Of course, you can easily impose limits if you want to exclude any worksheets or pivot tables. The nice thing about this is not only does it do all tables in blocks (by sheet), but if tables are added, removed or renamed, it doesn't crash when looking for specific tables.

as of excel 2016 (i think, certainly 2019)
ThisWorkbook.RefreshAll

Related

Excel VBA re-apply pivot table data source

I have a macro that copies two sheets from my workbook to their own workbook. One sheet has some data defined with a named range, the second has multiple pivot tables, all with the data source as said named range candData.
Once the sheets have copied my slicers lose some of their Report Connections.
In order to reconnect them, I have to manually select each pivot table and set the data source again, even though the data source is already set. (I simply click Change Data Source and immediately click OK, without actually changing anything.)
Once I have done this for each pivot table all report connections are showing again.
I have the following macro to loop through each pivot table and re apply the data source, then reconnect each slicer connection, however even after the data source has been applied, I still have to manually select each pivot table in order to reconnect the slicers.
I am getting no errors, it steps through each pivot table as expected and resets the data source. Any clues why the report connections only work when I set the data source manually instead of through the following?
Sub setSlicerSource()
Dim MyPivot As PivotTable
Dim slCaches As SlicerCaches
Dim slCache As SlicerCache
Set slCaches = ActiveWorkbook.SlicerCaches
With ActiveWorkbook
For Each MyPivot In .Sheets("Pivots").PivotTables
MyPivot.ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")
Next MyPivot
For Each slCache In slCaches
For Each MyPivot In .Sheets("Pivots").PivotTables
slCache.PivotTables.AddPivotTable MyPivot
Next MyPivot
Next slCache
End With
End Sub
I'm not sure if this will actually solve your problem, but it's the first thing I'd try fixing regardless:
This line currently creates a new PivotCache for each PivotTable:
MyPivot.ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")
Instead, create a PivotCache for PivotTables(1) first before the loop:
.Sheets("Pivots").PivotTables(1).ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")
Then in the loop, set all PivotTables to use the cache of PivotTables(1)
MyPivot.ChangePivotCache .Sheets("Pivots").PivotTables(1)

Refresh pivot tables but not external data source

I have a spreadsheet with multiple tables, where the data is pulled from an external data source (SQL database). The connections/tables refresh by changing an option in a drop down box and then pressing a button to run a VBA.
Attached to each of these tables is a pivot table. The pivot tables don't refresh with the tables. If I try pressing refresh all I get the error;
'Data Source name not found and no default driver specified'
However if I go through the spreadsheet and hit refresh on each individual pivot table they update without the error.
So either I need some way to get the pivot tables to refresh with the tables or have a button that refreshes only the pivot tables and not the external data connections.
Any ideas appreciated, I don't know where to begin with this one!
You can refresh a given PivotTable on Sheet1 like this:
Sheet1.PivotTables(1).RefreshTable
That will refresh the first PivotTable on Sheet1. Change the index number for a different one.
Or...
You can refresh all of the PivotTables on a give sheet by calling this routine:
Sub RefreshPivotTables(ws As Worksheet)
Dim pt As PivotTable
For Each pt In ws.PivotTables
pt.RefreshTable
Next
End Sub
You would call the above routine from the same code associated with the button mentioned in your question that updates the tables.
Or...
If you'd like to update all of the PivotTables in a workbook, you can use this version of the routine:
Sub RefreshPivotTables(wb As Workbook)
Dim ws As Worksheet
Dim pt As PivotTable
For Each ws In wb.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next
Next
End Sub
You would call this version like so:
RefreshPivotTables ThisWorkbook
Becky: Any reason you don't populate those PivotTables directly from the SQL query? Unless you need those tables there for some reason I'd suggest just ditch 'em, and simply turn the data directly into PivotTables. Otherwise you're effectively saving the same data in the file twice. (Or three times, if you haven't unchecked "Save source data with file" under PivotTable>Data>Options.
If you do need to refresh them - and if there are multiple PivotTables connected to each Table - then it is more efficient to iterate through the underlying PivotCaches and refresh any where the sourcetype is an Excel Range. In VBA speak that's where pc.SourceType = xlDatabase
Sub Refresh_PivotCaches()
Dim pc As PivotCache
For Each pc In ActiveWorkbook.PivotCaches
If pc.SourceType = xlDatabase Then pc.Refresh
Next pc
End Sub
If you instead iterate through each and every PivotTable, then if multiple PivotTables are connected to one PivotCache you end up doing more refreshes than you need. For instance, if you have 10 PivotTables that all point to the same table, you do not need to refresh those 10 PivotTables individually. Rather, you just need to refresh the one PivotCache that they all share. If you were to refresh those 10 PivotTables individually, then in effect you are refreshing each of those 10 PivotTables 10 times.
Granted, if your PivotTables are small you won't notice any difference between my code and Excel Heros.
I added the following at the end of my code which seemed to work fine.
Dim PT As PivotTable
Dim WSH As Worksheet
For Each WSH In ThisWorkbook.Worksheets
For Each PT In WSH.PivotTables
PT.RefreshTable
Next PT
Next WSH
I know this is an old post, but sharing in case this helps anyone researching this like I was -- This may be new to Excel 2016 (I don't have access to older versions currently to test), however I found that you can disable the "Refresh with Refresh All" setting on your external data sources. This will then allow you to use the "Refresh All" from the Data Tab to update all your pivot tables at once, without updating the external data source query again:
Data Tab > Connections Section > Open Connections
Select a connection in the Workbook Connections window and click Properties...
In the properties window, uncheck the box for "Refresh this connection on Refresh All"
Repeat for any additional External Connections you don't want to update automatically
I use this in a reporting macro by calling the manual query updates once:
(selecting a cell in each table created by a query)
ActiveWorkbook.Sheets("Completed").Select
Range("A2").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
And then calling refresh all as needed to update my pivot tables:
ActiveWorkbook.RefreshAll

Creating Pivot table and chart with multiple worksheets

I'm facing a problem regarding creating Pivot table and chart with multiple worksheets.
I have already created a pivot table and chart using Microsoft Query. I followed the steps from this blog:
http://www.ashishmathur.com/create-a-pivot-table-from-multiple-worksheets-in-the-same-workbook/
Right now, I'm figuring out how I can add new data into pivot table.
Is it possible to add new worksheets of data into already created query and pivot table?
Or do I need to create all over again everytime I add new worksheet of data?
FYI: I'm using Excel 2007 and all my data contains same no. of columns and same column names.
Looks like you need to add just one more block of UNIONALL Select * from dummy2.
If you'd need to do it say weekly or daily I recommend getting to know basic macro editing that will enable you to pull data from numerous sheets to a summary one and base your SQL on that single sheet.
Sub loop_through_sheets()
Dim ws As Worksheet 'define a worksheet
For Each ws In ActiveWorkbook.Worksheets 'for each worksheet
If ws.Name Like "YourString" Then 'make sure you don't consolidate the summary sheet
Call DataGrab(ws)
End If
Next ws
End Sub

How do you refresh Slicers in a single sheet?

I'd like to clear the filters of all slicers within a sheet but leave the other slicers "un-refreshed".
I have seen the following code that refreshes all slicers within the workbook but can't get it to work solely on a particular sheet only.
Dim slcr As SlicerCache
For Each slcr In ActiveWorkbook.SlicerCaches
slcr.ClearManualFilter
Next slcr
Any help gratefully received!
Thanks
After a bit of playing around and testing (never used slicers through VBA before) I worked out the below.
However, be advised that a slicercache can have slicers on different worksheets. A slicer is linked to a slicer cache which in turn is linked to a data source (e.g. a pivot table). If any slicer is changed, then the slicer cache is changed which changes the pivot table as well. You cannot have 1 slicer with a filter on and another slicer linked to the same pivot table with a filter off, as the pivot table is updated by a slicer's filters. It would't make sense. ;-)
What does make sense is having multiple slicers and multiple pivot tables - using the below will indeed clear the filters for one slicer / cache / pivot but not change any others. This is the only way it could work so hopefully that was what you wanted anyway!!
Sub test()
RefreshSlicersOnWorksheet ActiveSheet
End Sub
Public Sub RefreshSlicersOnWorksheet(ws As Worksheet)
Dim sc As SlicerCache
Dim scs As SlicerCaches
Dim slice As Slicer
Set scs = ws.Parent.SlicerCaches
If Not scs Is Nothing Then
For Each sc In scs
For Each slice In sc.Slicers
If slice.Shape.Parent Is ws Then
sc.ClearManualFilter
Exit For 'unnecessary to check the other slicers of the slicer cache
End If
Next slice
Next sc
End If
End Sub

Displaying a list of pivot tables with their parameters

I have a workbook with many worksheets with many pivot tables. Having an overview in one place of all these pivot tables would be great to ensure I have consistent parameters, titles, etc.
It should display key info such as:
Worksheet name, Pivot Table Name, Pivot Table location, Hyperlink to location, chose parameters (such as OLAP support, current connected slicers, etc., Type (power pivot or standard)
I guess we would all be happy to have this for all our complex applications.
Would anyone have a clue on how to design this in VBA or something else?
You should be able to loop through pivot tables and grab information with something like:
Dim pvt As PivotTable
Dim ws As Worksheet
Set ws = ThisWorkBook.ActiveSheet
For Each pvt In ws.PivotTables
'collect data about pivot tables and output somewhere..
Next pvt
if the pivot tables are in multiple worksheets then nest that loop inside a loop that cycles through the workbooks worksheets.
Does that provide an adequate starting point for you?

Resources