I'm trying to generate a pivot table from a table (listobject) named ProjectList on Sheet3 of my workbook.
What I've got is as follows, but I get an error message (1004) when I try to run it.
Dim PivotTableCache As PivotCache
Dim PT As PivotTable
Worksheets("Pivot Table Category").Delete
Sheets.Add
ActiveSheet.Name = "Pivot Table Category"
Set PivotTableCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=Range("ProjectList"))
Sheets("Pivot Table Category").Select
Set PT = PivotTableCache.CreatePivotTable(TableDestination:=Range(1, 1), _
TableName:="PivotCategory")
`
I've also tried a variation where I drop in the following at the beginning
Dim PRange As Range
Set PRange = Sheet3.ListObjects("ProjectList").DataBodyRange
and refer to PRange when setting up the PivotCache, but that doesn't work either.
Any ideas where I'm going wrong? I have a vague notion that it may be related to the sourcetype.
Your range reference for the destination is incorrect. It should be
Set PT = PivotTableCache.CreatePivotTable(TableDestination:=Cells(1, 1), TableName:="PivotCategory")
'OR
Set PT = PivotTableCache.CreatePivotTable(TableDestination:=Range("A1"), TableName:="PivotCategory")
One fell swoop can do it:
Dim PT as PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"ProjectList", Version:=xlPivotTableVersion14).CreatePivotTable (TableDestination _
:="Pivot Table Category!R1C1", TableName:="PivotCategory", DefaultVersion:= _
xlPivotTableVersion14)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.