我有一些代码可以创建一个数据透视表我想要它但是当我尝试添加一个数据透视过滤器来过滤前十个值我得到一个错误:
“运行时错误1004:应用程序定义的错误或对象定义的错误”
Sub Create_Port_Pivots(dataField, PivotLastRow, tableDest, counter)
'
' Creates pivot table on Pivot sheet
' Author: Daryl Findlay (dfind)
'
Dim objTable As PivotTable, objField As PivotField
' Select the source sheet and assign LastRow & LastCol
ActiveWorkbook.Sheets("Data").Select
LastRow = Range("A3").End(xlDown).Row
LastCol = Range("A3").End(xlToRight).Column
' Create the PivotTable object.
Set objTable = ActiveSheet.PivotTableWizard( _
SourceType:=xlDatabase, _
sourceData:=ActiveSheet.Name & "!R1C1:R" & LastRow & "C" & LastCol, _
TableDestination:="Pivot!R" & PivotLastRow & tableDest, _
TableName:="Pivot" & counter)
' Specify row and column fields.
Set objField = objTable.PivotFields("Date")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Time Group")
objField.Orientation = xlColumnField
Set objField = objTable.PivotFields("Port Name")
objField.Orientation = xlRowField
' Specify a data field with its summary.
Set objField = objTable.PivotFields(dataField)
objField.Orientation = xlDataField
objField.Function = xlAverage
' ----------------- Filter by top 10 - Error with second param
objField.PivotFilters.Add xlTopCount, objTable.PivotFields("Port Name"), 10
End Sub
调试将此行突出显示为问题:
objField.PivotFilters.Add xlTopCount, objTable.PivotFields("Port Name"), 10
我认为问题可能出在该函数调用的第二个参数上。任何帮助将不胜感激。
答案 0 :(得分:0)
在与同事交谈后,我发现了另一种方法。
ActiveSheet.PivotTables("Pivot" & counter).PivotFields("Port Name").AutoShow xlAutomatic _
, xlTop, 10, "Average of Max I/O /sec"
将objField.PivotFilters.Add xlTopCount, objTable.PivotFields("Port Name"), 10
替换为上面的代码行,它只显示前10个值