Change Data source of Pivot Table from VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
848
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am looking to change the data source of my VBA. But getting syntax error when doing the below. What am I doing wrong? I can't even get it to work for a simple A2 - V100. Eventually I want the 100 to be set by my lr.

My data will always start on A2 to V but the amount of data could change that is why I am trying to use LR.

VBA Code:
lr = Cells(rows.count, "B").End(xlUp).row

Sheets("Pivot Table").PivotTables("PivotTable1").ChangePivotCache Sheets("Pivot Table").
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Transaction!$A$2:$V$100", Version:=8)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Pivotcaches is a workbook property, not a worksheet one:

Code:
Sheets("Pivot Table").PivotTables("PivotTable1").ChangePivotCache _ 
        activeworkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="Transaction!$A$2:$V$100", Version:=8)
 
Upvote 0
getting an error just not sure why. I copied what you had and it looks fine to me

1711381472033.png
1711381505207.png
 
Upvote 0
now its a runtime 1004 if i change it to ActiveWorkbook from Sheets("Pivot Table")

VBA Code:
Sheets("Pivot Table").Activate
ActiveWorkbook.PivotTables("PivotTable1").ChangePivotCache _
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="Transaction!$A$2:$V$100", Version:=8)
 
Upvote 0
I just wanted to say i got it to work. researched a bit online to help. took a lot of trial and error but got there see below if anyone needs it for any future dilemmas.

VBA Code:
Dim lr As Long
Dim SrcData As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ActiveSheet.AutoFilterMode = False

If Not ActiveSheet.AutoFilterMode Then ActiveSheet.Range("10:10").AutoFilter

With ActiveWindow
    .ScrollRow = 1 'the row you want to scroll to
    .ScrollColumn = 1 'the column you want to scroll to
    .SplitColumn = 9
    .SplitRow = 10
    .FreezePanes = True
End With

lr = Cells(rows.count, "B").End(xlUp).row

SrcData = "'" & ActiveSheet.Name & "'" & "!" & Range("$A$10:$V$" & lr).Address(ReferenceStyle:=xlA1)

Sheets("Pivot Table").Activate
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData, Version:=8)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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