对于多个数据透视图,VBA:对象'_Chart'的方法'SetSourceData'失败

时间:2017-05-24 19:15:22

标签: excel vba excel-vba

这是上一个问题的第2部分:

VBA: Runtime Error - SetSourceData for Second Pivot Chart

在遵循该主题中提供的(好)建议后,我仍然遇到同样的问题。在第二个图表尝试SetSourceData后,标题错误仍然显示。如果我添加一个On Error Resume Next,那么数据会变得不稳定且不可靠。我开始认为它是PivotCache的问题,但我试图用不同的变量名来区分缓存。这并没有解决问题。下面是完整的代码,其中包含一些标题/名称:

Option Explicit

Sub CreatePivots()

Call CreateBarPivot
Call CreatePiePivot

End Sub
Sub CreateBarPivot()

Dim myWB As Workbook
Dim PSheet, DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim ChartWidth As Range
Dim BPivot As Shape

'Define Workbook
Set myWB = ThisWorkbook

'Define worksheets
Set PSheet = myWB.Sheets("Tools")
Set DSheet = myWB.Sheets("Aggregate")

'Define last data points
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column

'Selects first to last filled row, and first to last filled column for data
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Create pivot cache
Set PCache = myWB.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange)

'Create pivot table
Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Range("A1"), _
        TableName:="ExcPT")

'Create pivot chart
Set BPivot = PSheet.Shapes.AddChart
    BPivot.Chart.SetSourceData Source:=Range("$A$1:$C$18"), PlotBy:=xlRows
    BPivot.Chart.ChartType = xlColumnStacked

ActiveWorkbook.ShowPivotTableFieldList = False

With BPivot.Chart.PivotLayout.PivotTable.PivotFields("redacted")
    .Orientation = xlPageField
    .Position = 1
End With

With BPivot.Chart.PivotLayout.PivotTable.PivotFields("redacted")
    .Orientation = xlColumnField
    .Position = 1
End With

With BPivot.Chart.PivotLayout.PivotTable.PivotFields("redacted")
    .Orientation = xlRowField
   .Position = 1
End With

With BPivot.Chart.PivotLayout.PivotTable.PivotFields("redacted")
    .Orientation = xlRowField
    .Position = 2
End With

'Insert Data
With PSheet.PivotTables("ExcPT").PivotFields("Exception")
    .Orientation = xlDataField
    .Position = 1
    .Caption = "Exception Status Count"
    .Function = xlCount
End With

'Hide Not Due
With BPivot.Chart.PivotLayout.PivotTable.PivotFields("Exception Status")
    .PivotItems("Not due").Visible = False
End With

'Move bar chart to Dashboard; resize
Set ChartWidth = Sheets("Dashboard").Range("B2:L25")
With BPivot.Chart.Parent
    .Height = ChartWidth.Height
    .Width = ChartWidth.Width
    .Top = ChartWidth.Top
    .Left = ChartWidth.Left
End With

BPivot.Chart.ChartArea.Select
BPivot.Chart.Location Where:=xlLocationAsObject, Name:="Dashboard"


End Sub

Sub CreatePiePivot()

Dim myWB As Workbook
Dim PSheet, DSheet As Worksheet
Dim PCache1 As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim ChartWidth As Range
Dim PPivot As Shape

'Define Workbook
Set myWB = ThisWorkbook

'Define worksheets
Set PSheet = myWB.Sheets("Tools")
Set DSheet = myWB.Sheets("Aggregate")

'Define last data points
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column

'Selects first to last filled row, and first to last filled column for data
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Create pivot cache
Set PCache1 = myWB.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange)

'Create pivot table
Set PTable = PCache1.CreatePivotTable _
    (TableDestination:=PSheet.Range("F1"), _
        TableName:="ExcPT1")

'Create pivot chart
Set PPivot = PSheet.Shapes.AddChart
    PPivot.Chart.SetSourceData Source:=Range("$F$1:$H$18"), PlotBy:=xlRows
    PPivot.Chart.ChartType = xlPie

ActiveWorkbook.ShowPivotTableFieldList = False

'Insert row
With PPivot.Chart.PivotLayout.PivotTable.PivotFields("Exception")
    .Orientation = xlRowField
   .Position = 1
End With

'Insert Data
With PSheet.PivotTables("ExcPT1").PivotFields("Exception")
    .Orientation = xlDataField
    .Position = 1
    .Caption = "Exception Status Count"
    .Function = xlCount
End With

'Hide Not Due
With PPivot.Chart.PivotLayout.PivotTable.PivotFields("Exception Status")
    .PivotItems("Not due").Visible = False
End With

'Move pie chart to Dashboard; resize
Set ChartWidth = Sheets("Dashboard").Range("B26:L49")
With PPivot.Chart.Parent
    .Height = ChartWidth.Height
    .Width = ChartWidth.Width
    .Top = ChartWidth.Top
    .Left = ChartWidth.Left
End With

PPivot.Chart.ChartArea.Select
PPivot.Chart.Location Where:=xlLocationAsObject, Name:="Dashboard"

With ActiveChart
    .HasTitle = False
End With

End Sub

2 个答案:

答案 0 :(得分:0)

我也有这个问题,在Excel 2016 64位版本上。使用上面的技术解决它来创建一个形状对象,而不是图表对象:

Dim PChart as shape
Set PChart  = PSheet.Shapes.AddChart
    With PChart.Chart
        .ChartType = xl3DColumn
        .SetSourceData Source:=p.TableRange1, PlotBy:=xlRows
    End With

答案 1 :(得分:0)

我有类似的问题。我通过不设置任何变量来解决此问题,这些变量保存了数据透视表和以前使用的数据透视表(在添加第二张图表之前)。