我正在vba中创建一个数据透视表。
我几乎成功地创造了它。下面是我的代码,
Sub Pivot()
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As pivottable
Set ws = Sheets("Cat_Pivot")
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Preparation Sheet!R1C7:R1048576C8")
Set pt = pc.CreatePivotTable(ws.range("B3"))
With pt
With .PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Colour")
.Orientation = xlColumnField
.Position = 1
End With
.AddDataField.PivotFields ("Colour"), "count of colour", xlCount
End With
End Sub
当我添加行
时,我得到一个对象必需的错误.AddDataField.PivotFields(" Color"),"颜色计数",xlCount
在添加此行之前,我可以创建一个数据透视表。我怎么能纠正这个?
我经历了可用的资源,据我所知,我已经通过dim Statement声明了变量。
答案 0 :(得分:1)
.AddDataField
声明中PivotFields ("Colour"), "count of colour", xlCount
和With pt
之间有一个空格。
改变你的:
.AddDataField.PivotFields ("Colour"), "count of colour", xlCount
为:
.AddDataField .PivotFields("Colour"), "Count of Colour", xlCount
没有With
声明:
pt.AddDataField pt.PivotFields("Colour"), "Count of Colour", xlCount