我正在尝试使用vba创建一个表格中的数据透视表

时间:2016-06-22 21:28:47

标签: mysql sql-server excel vba excel-vba

所以我从数据库中提取信息,然后把它放在一张桌子上。我想通过VBA汇总数据透视表上的所有信息。我试图自动刷新表,但它会给我一个错误,或者它只会获得一些信息。现在我正在尝试使用VBA创建一个数据透视表,但是我得到错误"对象变量或者没有设置块变量"这是我的代码:

Sub sbCreateCalculatedField()

Sub sbCreateCalculatedField()

'Declaration
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
'Adding new worksheet
Set ws = Worksheets.Add
'Creating Pivot cache
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase,)
'Creating Pivot table
Set pt = pc.CreatePivotTable(ws.Range("B3"))
'Setting Fields
With pt
'set row field
With .PivotFields("CATEGORY")
.Orientation = xlRowField
.Position = 3
End With
'set column field
With .PivotFields("DIFF")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("RUN_DATE")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("TYPE")
.Orientation = xlRowField
.Position = 3
End With
With .PivotFields("CATEGORY")
.Orientation = xlColumnField
.Position = 2
End With
With .PivotFields("TYPE")
.Orientation = xlColumnField
.Position = 2
End With

'set data field
.AddDataField .PivotFields("DIFF"), "Sum of DIFF", xlSum
End With
End Sub

0 个答案:

没有答案