在VBA中创建数据透视表:运行时错误“1004”(无法获取工作表类的数据透视表属性)
我正在使用Excel 2010,并使用以下VBA代码创建透视表:-在VBA中创建数据透视表:运行时错误“1004”(无法获取工作表类的数据透视表属性),vba,excel,excel-2010,Vba,Excel,Excel 2010,我正在使用Excel 2010,并使用以下VBA代码创建透视表:- Sub addFields() With ActiveSheet.PivotTables(1) With .PivotFields("Name") .Orientation = xlRowField .Position = 1 End With With .PivotFields("Location") .Orientation = xlPageField
Sub addFields()
With ActiveSheet.PivotTables(1)
With .PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Location")
.Orientation = xlPageField
.Position = 1
End With
.AddDataField ActiveSheet.PivotTables(1).PivotFields("Order Amount"), _
"Sum of Amount", xlSum
End With
End Sub
下面是Sheet1上的数据,我想在其中创建透视表
Name Order Amount Location
Grace 1200000 New York
Tom 2000000 New York
Madhuri 5000000 Delhi
Hari 6000000 Delhi
Raj 7000000 Delhi
Victoria 8000000 New York
但我得到了一个:
运行时错误1004:无法获取工作表类的数据透视表属性
有人可以就此提出建议吗?错误发生在哪一行?您已经在尝试处理透视表对象,但尚未创建它。首先需要创建透视表缓存,然后才能使用上述代码。