VBA: Method 'Location' of object '_Cha

2019-08-03 02:51发布

VBA is throwing the "Method 'Location' of object '_Chart' failed" error when I try to run the CreatePivots sub from the below code:

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

I have been solving a similar issues described here:

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

..thanks to the helpful comments/answers shown. After reviewing the line-by-line, I'm not seeing where Excel can't figure out where I mean to put this chart. After executing the sub, the chart does end up in the right spot, but the error is there. It usually occurs on the second location move:

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

Additionally, it doesn't seem to happen when I have the Dashboard tab already open in excel, though I only tested this two or three times. It does seem to happen every time if the Dashboard tab is not open, however.

0条回答
登录 后发表回答