创建数据透视表

时间:2017-06-28 13:03:19

标签: excel vba excel-vba

我正在使用VBA代码生成数据透视表,我的代码适用于其他工作表,但在此工作表中始终会产生错误。我检查了列名称,表格名称,但我无法弄清楚我在哪里犯错。我甚至记录了宏观交叉检查,但我仍然无能为力。

Sub AutoPivot7()

Dim ws7 As Worksheet
Dim pc7 As PivotCache
Dim pt7 As PivotTable
Dim ct7 As Integer
Set ws7 = Sheets("Pivot_Reasons")


Set pc7 = ActiveWorkbook.PivotCaches.Create(xlDatabase, "'Reasons for Delay'!R1C1:R1048576C2")

Set pt7 = pc7.CreatePivotTable(ws7.Range("B3"))

pt7.AddDataField pt7.PivotFields("Sum of Reasons for Delay"), "Sum of Reasons for Delay", xlSum

With pt7

With .PivotFields("Reasons for Delay")
.Orientation = xlRowField
.Position = 1
.AutoSort xlDescending, "Sum of Reasons for Delay"

End With

With .PivotFields("Sum of Reasons for Delay")
.Calculation = xlPercentOfTotal
End With
End With
End Sub

错误发生在

行中
  

pt7.AddDataField pt7.PivotFields(“延迟原因总和”),“总和   延迟的原因“,xlSum

如果我删除它并运行我的代码,我没有收到错误。我也附上了我提到的记录宏

Sub Macro7()
'
' Macro7 Macro
'

'
    ActiveCell.Offset(-21, -1).Range("Table3[#All]").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Table3", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination _
        :="Sheet1!R3C1", TableName:="PivotTable44", DefaultVersion:= _
        xlPivotTableVersion15
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable44").PivotFields("Reasons for Delay")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable44").AddDataField ActiveSheet.PivotTables( _
        "PivotTable44").PivotFields("Sum of Reasons for Delay"), _
        "Sum of Sum of Reasons for Delay", xlSum
    ActiveSheet.PivotTables("PivotTable44").PivotFields("Reasons for Delay"). _
        AutoSort xlDescending, "Sum of Sum of Reasons for Delay"
    ActiveCell.Offset(0, 1).Range("A1").Select
    With ActiveSheet.PivotTables("PivotTable44").PivotFields( _
        "Sum of Sum of Reasons for Delay")
        .Calculation = xlPercentOfTotal
        .NumberFormat = "0,00%"
    End With
End Sub

2 个答案:

答案 0 :(得分:0)

更改此行: pt7.AddDataField pt7.PivotFields(“延迟原因总和”),“延迟原因总和”,xlSum 至: pt7.AddDataField pt7.PivotFields(“延迟原因总和”),“延迟原因”,xlSum

答案 1 :(得分:0)

尝试以下代码:

Sub CreatePivotTable()
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
With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "!!! Please Be Patient...Updating Records !!!"
    .EnableEvents = False
    .Calculation = xlManual
End With
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Pivot_Reasons")
'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(2, 2), _
TableName:="FilteredPivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), 
TableName:="FilteredPivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Reasons for 
Delay")
 .Orientation = xlRowField
 .Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Reasons for 
Delay")
 .Orientation = xlDataField
 .Position = 1
 .Function = xlSum
 .NumberFormat = "#,##0"
 .Name = "Reasons"
End With

'Format Pivot Table
ActiveSheet.PivotTables("FilteredPivotTable").CompactLayoutRowHeader = "Reasons"
ActiveSheet.PivotTables("FilteredPivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("FilteredPivotTable").TableStyle2 = "PivotStyleMedium9"
With Application
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .StatusBar = False
    .EnableEvents = True
    .Calculation = xlAutomatic
End With
End Sub