Creating Pivot Table - Problem with my Pivot Cache

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
250
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I will start by saying I don't fully understand pivot tables but with the excellent help on here, I got one working in my old project. I am trying to replicate it now in a new project.

I want to create a PivotTable on a worksheet, using a table on another as its data source.

My "old project" looks like this:

VBA Code:
LRow = WS2.Range("A1").End(xlDown).Row
LCol = WS2.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = WS2.Cells(1, 1).Resize(LRow, LCol)

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=WS1.Cells(4, 1), TableName:="FinalPivot")
Set PTable = PCache.CreatePivotTable(TableDestination:=WS1.Cells(1, 1), TableName:="FinalPivot")

I tried to rewrite it like this:
VBA Code:
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Ws2.Listobjects(1)).CreatePivotTable(TableDestination:=WS1.Cells(4, 1), TableName:="FinalPivot")
Set PTable = PCache.CreatePivotTable(TableDestination:=WS1.Cells(1, 1), TableName:="FinalPivot")

Here, instead of setting a Range on the source workbook, I just try to use the whole table instead. That failed with "Application-defined or object-defined error".

So I went back to the original code above, setting the Range as variable PRange using Last Row and Last Column parameters and I get the same error.

What is confusing me here is this is an exact copy/paste of existing code which works fine.

Can anybody see anything obvious here?

Edit to add:

The table is created, and the Data Source within the Excel program itself is absolutely correct.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try the following...

VBA Code:
Dim PCache As PivotCache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Ws2.ListObjects(1).Range)

Dim PTable As PivotTable
Set PTable = WS1.PivotTables.Add(PivotCache:=PCache, TableDestination:=WS1.Cells(1, 1), TableName:="FinalPivot")

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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