我正在使用vba创建数据透视表。该表有一些过滤器,我无法从过滤器中选择多个项目。
我录制了一个宏并使用了与系统生成的代码相同的功能,但我得到了
"Run-time error '1004': Unable to set the CurrentPage Property of the PivotField Class"
以下是用于创建数据透视表的代码:
Dim objTable As PivotTable
Dim objField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("All Projects - iNexus").Select
Range("A1").Select
' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = ActiveSheet.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Product")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("SPA Value Category")
objField.Orientation = xlColumnField
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("2014 FY Value ($)")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "$ #,##0"
Set objField = objTable.PivotFields("2015 FY Value ($)")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "$ #,##0"
ActiveSheet.Name = "iNexus Pivot"
' Rename the pivot table
With Sheets("iNexus Pivot")
.PivotTables(1).Name = "PivotTable2"
End With
With Sheets("iNexus Pivot").PivotTables("PivotTable2").DataPivotField
.Orientation = xlColumnField
End With
这是我从录制的宏中获取的代码,用于从录制的宏中选择多个项目:
ActiveSheet.PivotTables("PivotTable2").PivotFields("Project Status"). _
CurrentPage = "(All)"
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Project Status")
.PivotItems("A").Visible = False
.PivotItems("B").Visible = False
.PivotItems("C").Visible = False
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("Project Status"). _
EnableMultiplePageItems = True
CurrentPage和PivotItems似乎都是问题的根源,我的vba技能非常有限,我无法调试问题。
感谢。感谢帮助。
答案 0 :(得分:0)
我发现了问题,解决方案是在使用以下命令分配方向时包含过滤器:
objField.PivotItems("XXX").Visible = False
我从下面的链接找到了逻辑。