【问题标题】:How can I change the source data range selected within a Pivot Table for use in a Pivot Chart如何更改在数据透视表中选择的源数据范围以在数据透视图中使用
【发布时间】:2015-05-14 08:31:16
【问题描述】:

目前我的数据透视表看起来像这样

目前,我创建的其中一个图表向 xlPie 图表添加了一个系列,但是它正在选择 Range("C6:C12") 中的数据,我希望它选择倒数第二列 Range("F6":"F12"),因为我们想要总值,而不仅仅是一个月的值。

这是当前创建的饼图供参考:

要创建此图表,我在此图表所在的工作表中使用以下代码:

Dim shpPie As Shape
Set shpPie = ActiveSheet.Shapes.AddChart(xlPie)

shpPie.Chart.SetSourceData Source:=ActiveWorkbook.Sheets("Pivot Table").PivotTables(1).TableRange1, _
                      PlotBy:=xlColumns
With Range("A1:F32")
    shpPie.Left = .Left
    shpPie.Top = .Top
    shpPie.Width = .Width
    shpPie.Height = .Height
End With
With shpPie.Chart.PivotLayout.PivotTable
    .PivotFields("Marketspace").Orientation = xlRowField 'Row Field is the Axis Fields
    .PivotFields("Page Name").Orientation = xlRowField
    .PivotFields("Year").Orientation = xlColumnField
    .PivotFields("Month").Orientation = xlColumnField 'Column Field is the Legend Fields
End With

我认为问题出在这一行:

 shpPie.Chart.SetSourceData Source:=ActiveWorkbook.Sheets("Pivot Table").PivotTables(1).TableRange1, _
                          PlotBy:=xlColumns

在 excel 中,该行为我提供了以下源数据选择:

=SERIES('Pivot Table'!$C$3:$C$5,'Pivot Table'!$A$6:$B$14,'Pivot Table'!$C$6:$C$14,1)

有谁知道我可以做些什么让它选择倒数第二列,或者其他技术来做到这一点?我希望它选择 2015 年的总数而不是 1 月的数据。

我已尝试更改从以下位置生成的源代码行:

=SERIES('Pivot Table'!$C$3:$C$5,'Pivot Table'!$A$6:$B$14,'Pivot Table'!$C$6:$C$14,1) 

到:

=SERIES('Pivot Table'!$C$3:$C$5,'Pivot Table'!$A$6:$B$14,'Pivot Table'!$F$6:$F$14,1)

只是想看看会发生什么,但它不允许我应用该更改。

我是 VB 新手,所以我可能会误解这些数据透视表/图表的工作原理

【问题讨论】:

  • 您无法更改数据透视图使用的数据。您必须创建一个常规图表,或使用仅显示 2015 年总数据的数据透视表副本。
  • Aaah 好的,在这种情况下,我将提取这些数据以创建新图表 :) 谢谢您的回复,@Rory

标签: excel pivot-table vba


【解决方案1】:

无需提取数据来创建新图表,只需复制包含数据透视表的工作表(按照 Rory 的建议),从复制的数据透视表中的值中排除“月份”字段。

从您的代码中排除以下行

.PivotFields("Month").Orientation = xlColumnField 'Column Field is the Legend Fields

然后为复制的工作表运行您的程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多