首页 > 解决方案 > 使用两个不同的数据透视表拥有相同的图表信息

问题描述

大家早上好,

我正在尝试使用数据透视表创建两个图表。但是我有完全相同的两个图表,而columnfield图表的不一样。我在两个图表上都有相同的columnfield Evt F ,而不是图表 1上的Evt F图表 2上的Evt P。数据透视表和图表在同一张表graphe_clos上。

我希望我足够清楚谢谢您的时间和帮助

这是我创建表格和图表的宏部分。

Sub tracer ()

Set Table = Cache.CreatePivotTable(TableDestination:=graphe_clos.Cells(1, 1), TableName:="Terminator")
Set Table1 = Cache.CreatePivotTable(TableDestination:=graphe_clos.Cells(25, 1), TableName:="Terminator2")
With graphe_clos.Shapes.AddChart(204, xlColumnClustered).Chart
    .ClearToMatchStyle
    .ChartStyle = 257
end with 

With Table.PivotFields("Tranches")
      .Orientation = xlRowField
      .Position = 1
End With

With Table.PivotFields("Evt F")
      .Orientation = xlColumnField
      .Position = 1
      .PivotItems("(blank)").visible = False
End With

With graphe_clos.Shapes.AddChart(204, xlColumnClustered).Chart
    .ClearToMatchStyle
    .ChartStyle = 257
end with 

With Table1.PivotFields("Tranches")
      .Orientation = xlRowField
      .Position = 1
End With

With Table1.PivotFields("Evt P")
      .Orientation = xlColumnField
      .Position = 1
      .PivotItems("(blank)").visible = False
End With

End Sub

标签: vbaexcel

解决方案


这很可能是因为已使用/共享相同的缓存。您可以尝试以下方法:

Set pvtCache = wbSource.PivotCaches.create(SourceType:=xlDatabase, SourceData:=YourDataRngStr) 'data/table range, something like "A1:E100"
Set pvtCache = wbSource.PivotCaches.create(SourceType:=xlDatabase, SourceData:=YourDataRngStr) 'data/table range, something like "A1:E100"
Set Table = pvtCache.CreatePivotTable(TableDestination:=graphe_clos.Cells(1, 1), TableName:="Terminator")
Set Table1 = pvtCache.CreatePivotTable(TableDestination:=graphe_clos.Cells(25, 1), TableName:="Terminator2")

谢谢


推荐阅读