我正在尝试通过VBA创建一个数据透视图,但我收到标题中的错误。这是我第一次尝试使用CreatePivotChart方法,并且我已经尝试了所有可以想到的组合来尝试强制它运行。以下代码是错误开始之前运行的代码:
Option Explicit
Sub CreatePiePivot()
Dim myWB As Workbook
Dim PieTool, Dashboard, Aggregate As Worksheet
Dim PivotCache As PivotCache
Dim PieTable As PivotTable
Dim PiePivot As Shape
Dim PivotRange, ChartSize As Range
Dim LastRow, LastCol As Long
'Define Workbook
Set myWB = ThisWorkbook
'Define worksheets
Set PieTool = myWB.Sheets("Tools2")
Set Dashboard = myWB.Sheets("Dashboard")
Set Aggregate = myWB.Sheets("Aggregate")
'Define last data points
LastRow = Aggregate.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Aggregate.Cells(1, Columns.Count).End(xlToLeft).Column
'Define chart size
Set ChartSize = Dashboard.Range("B26:O50")
'Select first-last filled row&column for data
Set PivotRange = Aggregate.Cells(1, 1).Resize(LastRow, LastCol)
'Create pivot cache
Set PivotCache = myWB.PivotCaches.Create(xlDatabase, PivotRange)
'Create pivot table
Set PieTable = PivotCache.CreatePivotTable(PieTool.Range("A1"), "PieTable")
'Create pivot chart
Set PiePivot = PivotCache.CreatePivotChart(PieTool)
With PiePivot
.XlChartType = xlPie
.Left = ChartSize.Left
.Top = ChartSize.Top
.Width = ChartSize.Width
.Height = ChartSize.Height
End With
错误在此处逐步显示:
Set PiePivot = PivotCache.CreatePivotChart(PieTool)
我也尝试过:
Set PiePivot = PivotCache.CreatePivotChart( _
ChartLocation:=Dashboard, _
XlChartType:=xlPie, _
Left:=ChartSize.Left, _
Top:=ChartSize.Top, _
Width:=ChartSize.Width, _
Height:=ChartSize.Height)
......但是那引发了同样的错误。我还尝试取出" ChartLocation"," XlChartType"等,但错误仍然存在。
我做错了什么?