我有创建数据透视表的代码,但在此代码块上不断出现应用程序错误object not defined
:
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="PivotTable")
这是其余的代码:
Sub Incentive_Avg()
'File Paths
Dim Scope_File As String
Dim Total_Roster As String
Scope_File = "M:\ScopeTemplate.xlsx"
Total_Roster = "M:\RosterTemplate.xlsx"
'Open WB's
Dim Scope_WB As Workbook
Set Scope_WB = Workbooks.Open(Filename:=Scope_File)
Dim Scope_WS As Worksheet
Set Scope_WS = Scope_WB.Sheets(1)
Dim rng1 As Range
Dim rng2 As Range
Dim lc As Range
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Application.DisplayAlerts = False
With Scope_WS
.Range("A:B,D:P,R:BR,BT:DX,DZ:EO").EntireColumn.Delete
End With
'Insert a New Blank Worksheet
'On Error Resume Next
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Sheet1")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="PivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("PivotTable").PivotFields("MgrName_Level_02")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("PivotTable").PivotFields("Emplid")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("IncentiveAnnual_2018")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
.NumberFormat = "$#,##0"
End With
End Sub