如何解决创建枢轴表的VBA代码正在运行,但现在无法运行
任何帮助将不胜感激。下面的代码以前可以工作,现在不能。对于下面的另一个PIVOT表,我具有完全相同的代码,它可以完美地工作。我在这上面挠头。
'Pivot Tables
Sub TestPivot()
Sheets("Reported Sales").Select
'Declare Variables
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
'Declare Variables
Application.DisplayAlerts = False
Worksheets("Pivot Reported Sales").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Pivot Reported Sales"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot Reported Sales")
Set DSheet = Worksheets("Reported Sales")
'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:="Pivot Reported Sales")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(2,2),TableName:="Pivot Reported Sales")
'Insert Row Fields
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("PROD_BASE_CD")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("Product Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("Compensation Category")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("PROD_SALES_AMT")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "$#,##0"
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("INVC_DATE")
.Orientation = xlPageField
.Position = 1
End With
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。