Change source of Pivot Table and Add Pivot Chart for all Tables in a specific worksheet

ChrisWhite

New Member
Joined
Jan 28, 2016
Messages
8
Dear all,

what I am trying to achieve is to run through all Pivot Tables in 2 specific sheets and

1) Update the sources of all Pivot Tables to a new file
2) Add a Pivot Chart to all pivot Tables

I create the code below which basically does it job, but it seems very complicated to me and it a bit buggy (sometimes it works, sometimes it doesnt).
Also, all the Pivot Tables create individual Caches, although the tables refer to two caches, only.

Do you have any improvement suggestions or Ideas how to resolve the "cache" issue?

Best,
Christian




Sub Update_Pivot_Sources()


Dim pt As PivotTable
Dim token As String
Dim newco As String
Dim Sheets(1 To 2) As String
Dim I As Integer
Dim input_location As String
Dim PtRange As Range
Dim objChart As Chart
Dim chtSeries As SeriesCollection




input_location = Application.ActiveWorkbook.Path

Sheets(1) = "-C - Finance und Ops"
Sheets(2) = "-C - Investment"

newco = InputBox("Input company name:")
token = InputBox("Input two-digit company token:")


For I = 1 To 2
Worksheets(token & Sheets(I)).Activate
For Each pt In ActiveSheet.PivotTables
If I = 1 Then
' MsgBox ActiveSheet.Name
' MsgBox pt.Name
ActiveSheet.PivotTables(pt.Name).ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
input_location & "\[Input_" & newco & ".xlsx]" & token & "-I - Financial and Ops KPIs!$A:$F")

Set PtRange = pt.TableRange1
Set objChart = ActiveSheet.Shapes.AddChart.Chart

With objChart
.SetSourceData PtRange
.ChartType = xlColumnClustered
.ShowAllFieldButtons = True
End With
Else
' MsgBox ActiveSheet.Name
ActiveSheet.PivotTables(pt.Name).ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
input_location & "\[Input_" & newco & ".xlsx]" & token & "-I - Investment!$A:$E")

Set PtRange = pt.TableRange1
Set objChart = ActiveSheet.Shapes.AddChart.Chart

With objChart
.SetSourceData PtRange
.ChartType = xlColumnClustered
.ShowAllFieldButtons = True
End With
End If
Next pt
Next I
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,215,219
Messages
6,123,683
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top