同一个excel表中的两个数据透视表

时间:2017-10-18 08:41:23

标签: excel vba excel-vba pivot-table

我在源数据的同一张表中创建了一个数据透视表。现在,我想在具有相同数据源的同一工作表中创建第二个数据透视表,但在数据透视表中使用不同的打印数据(当然)。我不断收到错误,因为它表示数据透视表不能覆盖另一个数据透视表。但是,他们选择了创建数据透视表的范围,他们不会互相覆盖。我也手动测试了。此外,如果我记录过程,它只使用源数据的精确参考,我不想使用,因为lastrow和lastcolumn每天都在变化。只是提醒一下,这个宏非常适合创建第一个表。第二个表是问题。下面,我将提供我现在的代码。

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

2 个答案:

答案 0 :(得分:2)

当您插入第一个数据透视表并在填充第一个数据透视表之前插入另一个数据透视表时,第一个数据透视表的空透视表范围占用第二个数据透视表的目标单元格[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

答案 1 :(得分:0)

来自MSDN doc on pivot tables

  

重要”如果在Excel上定义多个数据透视表   工作表, 可能生成的表可以增长和重叠   如果Activity返回大型数据集,则彼此。在这种情况下,   您将收到“数据透视表报告不能与另一个重叠”   数据透视表报告“刷新数据时。您可以更正此问题   通过在数据透视表之间添加添加列或行来导致错误   允许表格在不重叠的情况下成长。