TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 252
- Office Version
- 365
- 2021
- Platform
- 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:
I tried to rewrite it like this:
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.
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.