两个数据透视表在同一个Excel表中

我已经在源数据的同一张表中创build了一个数据透视表。 现在,我想在同一张表中使用相同的数据源创build第二个数据透视表,但在数据透视表(当然)中使用不同的打印数据。 我一直收到一个错误,因为它说,数据透视表不能覆盖另一个数据透视表。 然而,他们的方式,我已经select了数据透视表创build的范围,他们不会相互覆盖。 我也手动testing。 另外,如果我logging过程,它只是使用源数据的准确参考,我不想使用,因为每天都会改变lastrow和lastcolumn。 只是提醒一下,这个macros很适合创build第一个表格。 第二个表是问题。 下面,我将提供我现在的代码。

Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable1 As PivotTable Dim PTable2 As PivotTable Dim PRange As Range Application.DisplayAlerts = True Set DSheet = Worksheets("Budget_Report") Set PRange = DSheet.Range(Cells(1, 53), Cells.SpecialCells(xlCellTypeLastCell)) Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) Set PTable1 = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(4, 1), TableName:="PivotTable1") Set PTable2 = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(17, 2), TableName:="PivotTable2") With ActiveSheet.PivotTables("PivotTable1") With .PivotFields("T-Lane") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True .Subtotals(1) = False End With With .PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlAverage .Caption = "Monthly Cost Forecast" End With End With With ActiveSheet.PivotTables("PivotTable2") With .PivotFields("Oran") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True .Subtotals(1) = False End With With .PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlAverage .Caption = "Monthly Cost Forecast" End With End With 

插入第一个数据透视表并在填充第一个数据透视表之前插入另一个数据透视表时,第一个数据透视表的空数据透视表范围将占用第二个数据透视表的目标单元格[DSheet.Cells(17,2)]。

我想你应该插入第一个数据透视表并填充它,然后插入另一个并填充。

看看是否能解决你的问题。

 Dim DSheet As Worksheet Dim PCache As PivotCache Dim PTable1 As PivotTable Dim PTable2 As PivotTable Dim PRange As Range Application.DisplayAlerts = True Set DSheet = Worksheets("Budget_Report") Set PRange = DSheet.Range(Cells(1, 53), Cells.SpecialCells(xlCellTypeLastCell)) Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange) Set PTable1 = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(4, 1), TableName:="PivotTable1") With ActiveSheet.PivotTables("PivotTable1") With .PivotFields("T-Lane") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True .Subtotals(1) = False End With With .PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlAverage .Caption = "Monthly Cost Forecast" End With End With Set PTable2 = PCache.CreatePivotTable(TableDestination:=DSheet.Cells(17, 2), TableName:="PivotTable2") With ActiveSheet.PivotTables("PivotTable2") With .PivotFields("Oran") .Orientation = xlRowField .Position = 1 .Subtotals(1) = True .Subtotals(1) = False End With With .PivotFields("Monthly Cost FCST") .Orientation = xlDataField .Position = 1 .Function = xlAverage .Caption = "Monthly Cost Forecast" End With End With 

从数据透视表的MSDN文档 :

重要 ”如果在Excel工作表上定义了多个数据透视表, 如果活动返回大型数据集, 可能会导致生成的表格互相叠加并重叠 。 在这种情况下,当您刷新数据时,您将收到“数据透视表报表无法重叠另一个数据透视表报表”。 您可以通过在数据透视表之间添加添加列或行来更正此错误,以使表增长不重叠。