Changing Data Source for Pivot Tables

Aaron DeYoung

New Member
Joined
Apr 23, 2014
Messages
12
I have been working through this code and I cannot figure out where I am going wrong. It runs through the code but it doesn't change the Pivot Tables data source. I am not using a Table, rather a sheet range for my data btw.

[Sub AdjustAllPivotDataRanges()
'PURPOSE: Dynamically change every pivot table's data source range in the workbook




Dim sht As Worksheet
Dim pvt As PivotTable
Dim StartPoint As Range
Dim rng As Range
Dim SourceAddress As String


'Enter Worksheet Name that holds your Pivot data source
Set sht = ActiveWorkbook.Worksheets("Completed Projects")


'Enter first cell in your Pivot data source
Set StartPoint = sht.Range("A5")


'Create SourceData address
Set rng = sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))
SourceAddress = sht.Name & "!" & rng.Address(ReferenceStyle:=xlR1C1)


'Loop through and update pivot tables with new data source range
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables

'Change Pivot Table's data source range address
pvt.ChangePivotCache _
ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SourceAddress)

'Ensure Pivot Table is refreshed
pvt.RefreshTable

Next pvt
Next sht


'Completion Message
MsgBox "All Pivot Table Data Source Ranges have been updated in this workbook!", vbInformation


End Sub]
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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