簡體   English   中英

VBA:對於多個數據透視圖,對象“_Chart”的方法“SetSourceData”失敗

[英]VBA: Method 'SetSourceData' of object '_Chart' failed For Multiple Pivot Charts

這是上一個問題的第 2 部分:

VBA:運行時錯誤 - 第二個數據透視圖的 SetSourceData

在遵循該線程中提供的(好的)建議之后,我仍然遇到同樣的問題。 在第二個圖表嘗試 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

我也有這個問題,在 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

我有類似的問題。 我通過將包含數據透視緩存和以前使用的數據透視表(在添加第二個圖表之前)的變量設置為空來解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM