Excel VBA数据透视项错误424
我正在编写以下代码以生成透视表。 PvtTb1.PivotFields(“Period”)中的每个pivot_项的Excel VBA数据透视项错误424,vba,excel,Vba,Excel,我正在编写以下代码以生成透视表。 PvtTb1.PivotFields(“Period”)中的每个pivot_项的。PivotItems这里有一个424错误,即“需要对象” 但是我不确定我丢失了什么东西。代码在没有标记的部分的情况下运行良好 我能修好这个吗 Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim wsPvtTbl As Works
。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
在
PvtTb1
中,末尾有“1”(一个数字),应该是“l”(一个字母)。在PvtTb1
中,末尾有“1”(一个数字),应该是“l”(一个字母)。因为您已经设置了PvtTbl
这一行的对象设置了PvtTbl=wsPvtTbl.pivottbl.数据透视表(“数据透视表1”)
,通过将与statemnet一起使用,您可以简化您的生活,避免将来出现类似于此处的错误(混合PvtTbl
和PvtTb1
),就像下面的较短版本(和“更干净”)代码一样
' modify the Pivot-Table properties, afeter you set it
With PvtTbl
.ManualUpdate = True
'add row, column and page (report filter) fields:
With .PivotFields("Period")
.Orientation = xlPageField
On Error Resume Next
.PivotItems("N").Visible = False
.PivotItems("Y").Visible = True
On Error GoTo 0
End With
Dim pivot_item As PivotItem
.PivotFields("Group").Orientation = xlRowField
With .PivotFields("Name")
.Orientation = xlRowField
.Position = 2
End With
For Each pivot_item In .PivotFields("Period").PivotItems
If pivot_item.Name = "Y" Then
.PivotFields("PROCESS_DATE").Orientation = xlColumnField
With .PivotFields("NO_REC")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
.Position = 1
End With
End If
Next pivot_item
End With
由于您已经用这一行Set PvtTbl=wsPvtTbl.PivotTables(“PivotTable1”)
设置了PvtTbl
对象,您可以简化您的生活,避免将来出现类似于这里的错误(将PvtTbl
和PvtTb1
),方法是将与statemnet一起使用,就像在较短的版本(和“清洁剂”)代码如下
' modify the Pivot-Table properties, afeter you set it
With PvtTbl
.ManualUpdate = True
'add row, column and page (report filter) fields:
With .PivotFields("Period")
.Orientation = xlPageField
On Error Resume Next
.PivotItems("N").Visible = False
.PivotItems("Y").Visible = True
On Error GoTo 0
End With
Dim pivot_item As PivotItem
.PivotFields("Group").Orientation = xlRowField
With .PivotFields("Name")
.Orientation = xlRowField
.Position = 2
End With
For Each pivot_item In .PivotFields("Period").PivotItems
If pivot_item.Name = "Y" Then
.PivotFields("PROCESS_DATE").Orientation = xlColumnField
With .PivotFields("NO_REC")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
.Position = 1
End With
End If
Next pivot_item
End With
我已经定义了PvtTb1。我后来也得到了PvtTb2,同样的代码在这个透视表上运行良好。哦,天哪,我明白你的意思了。我太愚蠢了!谢谢我定义了PvtTb1。我后来也得到了PvtTb2,同样的代码在这个透视表上运行良好。哦,天哪,我明白你的意思了。我太愚蠢了!谢谢