在VBA中创build数据透视表:运行时错误“1004”(无法获取Worksheet类的数据透视表属性)

我正在使用Excel 2010,并使用下面的VBA代码来创build数据透视表: –

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 

以下是我想要创build数据透视表的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:无法获取Worksheet类的PivotTables属性。

任何人都可以请build议呢?