我想制作一个显示各种位置IDS的数据透视表。我在Set pc行收到以下错误消息“应用程序-定义的或对象定义的错误”:
我不确定发生了什么问题,任何指导都将对您有帮助
子createpivot()
Dim pt As PivotTable
Dim pc As PivotCache
Dim pf As PivotField
Dim pi As PivotItem
Dim PRange As Range
Dim lastrow As Long
Dim lastcol As Long
Dim Dsheet As Worksheet
Dim Psheet As Worksheet
Set Psheet = Worksheets("Pivot Table")
Set Dsheet = Worksheets("Locations")
lastrow = Dsheet.Cells(Rows.count, 1).End(xlUp).Row
lastcol = Dsheet.Cells(1, Columns.count).End(xlToLeft).Column
Set PRange = Dsheet.Cells(8, 21).Resize(lastrow, lastcol)
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=Psheet.Cells(5, 5), TableName:="Locations Month over Month")
Set pt = pc.CreatePivotTable(TableDestination:=Psheet.Cells(5, 5), TableName:="Locations Month over Month")
With ActiveSheet.PivotTables("Locations Month over Month").PivotFields("Location IDs")
.Orientation = xlRowField
.Position = 1`enter code here`