VBA正在抛出"方法'位置'对象' _Chart'失败"我尝试从以下代码运行CreatePivots子时出错:
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 blank pivot table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Range("A1"), _
TableName:="ExcPT")
'Create blank pivot chart
Set BPivot = PSheet.Shapes.AddChart
BPivot.Chart.SetSourceData Source:=PSheet.Range("A1"), PlotBy:=xlRows
BPivot.Chart.ChartType = xlColumnStacked
With BPivot.Chart.PivotLayout.PivotTable.PivotFields("redacted")
.Orientation = xlPageField
.Position = 1
End With
With BPivot.Chart.PivotLayout.PivotTable.PivotFields("Exception Status")
.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:O25")
With BPivot.Chart.Parent
.Height = ChartWidth.Height
.Width = ChartWidth.Width
.Top = ChartWidth.Top
.Left = ChartWidth.Left
End With
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 PTable1 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 PTable1 = PCache1.CreatePivotTable _
(TableDestination:=PSheet.Range("F1"), _
TableName:="ExcPT1")
'Create pivot chart
Set PPivot = PSheet.Shapes.AddChart
PPivot.Chart.SetSourceData Source:=PSheet.Range("F1"), PlotBy:=xlRows
PPivot.Chart.ChartType = xlPie
myWB.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("B27:O50")
With PPivot.Chart.Parent
.Height = ChartWidth.Height
.Width = ChartWidth.Width
.Top = ChartWidth.Top
.Left = ChartWidth.Left
End With
PPivot.Chart.Location _
Where:=xlLocationAsObject, _
Name:="Dashboard"
With PPivot.Chart
.HasTitle = False
End With
End Sub
我一直在解决这里描述的类似问题:
VBA: Method 'SetSourceData' of object '_Chart' failed For Multiple Pivot Charts
..感谢所示的有用评论/答案。在逐行审核之后,我没有看到Excel在哪里可以找出放置此图表的意义。执行sub后,图表最终会出现在正确的位置,但错误就在那里。它通常发生在第二个位置移动:
PPivot.Chart.Location _
Where:=xlLocationAsObject, _
Name:="Dashboard"
此外,当我在Excel中已经打开仪表板选项卡时似乎不会发生这种情况,尽管我只测试了两到三次。但是,如果“仪表板”选项卡未打开,则每次都会发生这种情况。