我正在使用以下代码生成数据透视表。
For Each pivot_item In PvtTb1.PivotFields("Period").PivotItems
这里有424错误,这是“需要对象”。
但是我不确定我错过了什么对象。代码运行良好,没有标记的部分。
我能解决这个问题吗?
Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim rngData As Range
Dim PvtTblCache As PivotCache
Dim wsPvtTbl As Worksheet
Dim pvtFld As PivotField
Set wsData = Worksheets("Verify")
Set wsPvtTbl = Worksheets("Summary")
wsPvtTbl.Cells.Clear
wsPvtTbl.Cells.ColumnWidth = 10
For Each PvtTbl In wsPvtTbl.PivotTables
If MsgBox("Delete existing PivotTable!", vbYesNo) = vbYes Then
PvtTbl.TableRange2.Clear
End If
Next PvtTbl
Set PvtTblCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="Verify")
PvtTblCache.CreatePivotTable TableDestination:=wsPvtTbl.Range("B19"), _
TableName:="PivotTable1"
Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")
PvtTbl.ManualUpdate = True
'add row, column and page (report filter) fields:
Set pvtFld = PvtTbl.PivotFields("Period")
pvtFld.Orientation = xlPageField
With wsPvtTbl.PivotTables("PivotTable1").PivotFields("Period")
On Error Resume Next
.PivotItems("N").Visible = False
.PivotItems("Y").Visible = True
On Error GoTo 0
End With
Dim pivot_item As PivotItem
Set pvtFld = PvtTbl.PivotFields("Group")
pvtFld.Orientation = xlRowField
Set pvtFld = PvtTbl.PivotFields("Name")
pvtFld.Orientation = xlRowField
pvtFld.Position = 2
For Each pivot_item In PvtTb1.PivotFields("Period").PivotItems
If pivot_item.Name = "Y" Then
Set pvtFld = PvtTb1.PivotFields("PROCESS_DATE")
pvtFld.Orientation = xlColumnField
With PvtTbl.PivotFields("NO_REC")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
.Position = 1
End With
End If
Next pivot_item
答案 0 :(得分:2)
在PvtTb1
中你有" 1" (一,一个数字)在最后,应该是" l" (一封信)。
答案 1 :(得分:0)
由于您Set
PvtTbl
对象已经Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")
,因此您可以简化生活,并避免将来出现错误(混合PvtTbl
和{ {1}}),使用PvtTb1
statemnet,就像下面的较短版本(和#34; clean")代码一样。
With