自动创build数据透视表时出错

我创build了一个macros,通过索引匹配和一般计算创build一个包含一些值的表格。 这个表位于范围(“AA1:BA”和LastRow) – >我正在写LastRow,因为每天都在改变拉斯特罗。 我想创build一个数据透视表,它将位于单元格(9,1)中,并将创build一个初始表作为源代码。我构build了您在下面看到的代码,但它给了我一个错误,数据透视表字段名称在行上无效:

Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="PivotTable") 

下面是与数据透视表相关的整个代码。

  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 Application.DisplayAlerts = False Application.DisplayAlerts = True Set PSheet = Worksheets("Budget_Report") Set DSheet = Worksheets("Budget_Report") LastRow = DSheet.Cells(Rows.Count, 27).End(xlUp).Row LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column Set PRange = DSheet.Cells(1, 27).Resize(LastRow, LastCol) Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") With ActiveSheet.PivotTables("PivotTable").PivotFields("T-Lane") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True .Subtotals(1) = False End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlSum .Name = "Monthly Cost Forecast" End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Vol FCST") .Orientation = xlDataField .Position = 2 .Function = xlSum .Name = "Monthly Vol Forecast" End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly $/SU FCST") .Orientation = xlDataField .Position = 3 .Function = xlSum .Name = "Monthly $/SU Forecast" End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Cost Actuals") .Orientation = xlDataField .Position = 4 .Function = xlSum .Name = "Monthly Cost Actual" End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly Vol Actuals") .Orientation = xlDataField .Position = 5 .Function = xlSum .Name = "Monthly Vol Actual" End With With ActiveSheet.PivotTables("PivotTable").PivotFields("Monthly $/SU Actuals") .Orientation = xlDataField .Position = 6 .Function = xlSum .Name = "Monthly $/SU Actual" End With 

下面提供了构build数据透视表的loggingmacros(当然,它的具体范围并不是最后一列)

 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Budget_Report!R1C27:R279C53", Version:=6).CreatePivotTable TableDestination:="Budget_Report!R9C1", TableName:="PivotTable1", DefaultVersion:=6 Sheets("Budget_Report").Cells(9, 1).Select With ActiveSheet.PivotTables("PivotTable1").PivotFields("T-Lane") .Orientation = xlRowField .Position = 1 End With ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Cost FCST"), "Monthly Cost Forecast", xlSum ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Vol FCST"), "Monthly Vol Forecast", xlSum ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly $/SU FCST"), "Monthly $/SU Forecast", xlSum ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Cost Actuals"), "Monthly Cost Actual", xlSum ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly Vol Actuals"), "Monthly Vol Actual", xlSum ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Monthly $/SU Actuals"), "Monthly $/SU Actual", xlSum ActiveWorkbook.ShowPivotTableFieldList = False 

当设置PRange ,即使你并不总是知道结束地址是什么,只要你确定在目标范围之下/之外没有额外的数据,就可以使用Cells.SpecialCells方法来设置你的范围。

它看起来像这样:

 Set PRange = DSheet.Range(DSheet.Cells(1, 27), DSheet.Cells.SpecialCells(xlCellTypeLastCell)) 

此外,这是一个个人风格的问题,但我发现嵌套块有时会导致更多的可读代码。 例如:

 With ActiveSheet.PivotTables("PivotTable1") With .PivotFields("Field 1") .Caption = "My first field" .Function = xlSum End With With .PivotFields("Field 2") .Caption = "My second field" .Function = xlMax End With End With 

等等

最终的产品看起来像这样:

 Dim DSheet As Worksheet ', PSheet As Worksheet Dim PCache As PivotCache, PTable As PivotTable, PRange As Range 'Dim LastRow As Long, LastCol As Long 'Application.DisplayAlerts = False ' This is redundant since you're setting it to true below. Application.DisplayAlerts = True 'Set PSheet = Worksheets("Budget_Report") ' Set DSheet = Worksheets("Budget_Report") ' These two are Redundant - I am using only DSheet. 'LastRow = DSheet.Cells(Rows.Count, 27).End(xlUp).Row 'LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column Set PRange = DSheet.Range(Cells(1, 27),Cells.SpecialCells(xlCellTypeLastCell)) Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) Set PTable = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(9, 1), TableName:="PivotTable") With ActiveSheet.PivotTables("PivotTable") With .PivotFields("T-Lane") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True ' You're resetting this value in the next line, you can probably remove it. .Subtotals(1) = False End With With .PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlSum .Caption = "Monthly Cost Forecast" 'You want .Caption here, not .Name End With With .PivotFields("Monthly Vol FCST") .Orientation = xlDataField .Position = 2 .Function = xlSum .Caption = "Monthly Vol Forecast" End With With .PivotFields("Monthly $/SU FCST") .Orientation = xlDataField .Position = 3 .Function = xlSum .Caption = "Monthly $/SU Forecast" End With With .PivotFields("Monthly Cost Actuals") .Orientation = xlDataField .Position = 4 .Function = xlSum .Caption = "Monthly Cost Actual" End With With .PivotFields("Monthly Vol Actuals") .Orientation = xlDataField .Position = 5 .Function = xlSum .Caption = "Monthly Vol Actual" End With With .PivotFields("Monthly $/SU Actuals") .Orientation = xlDataField .Position = 6 .Function = xlSum .Caption = "Monthly $/SU Actual" End With End With 

看起来您尝试创build两个相同的表格:

 Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="PivotTable") Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") 

把它分成两部分:

 Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(9, 1), TableName:="PivotTable") 

仅分配给PCachecaching:

 Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)