仅显示pivottable excel中的顶级logging

我想通过vba筛选前10行的我的pivottable。 在图片中描绘了手动的方式,以明确我的意思。

在这里输入图像说明

到目前为止我已经尝试了以下方法,但是我无法得到这个工作。 (ObjField在这里指的是创builddate。)

objField.AutoShow Type:=xlAutomatic, Range:=xlTop, Count:=10, Field:="Count of Creation Date" 

我确定objfield引用本身是正确的,因为通过代码对pivottable的其他调整工作得很好。 不过,我在这里使用了错误的pivotfield。 任何帮助这个小问题将不胜感激!

编辑:

完整的代码; 从访问中调用:

 Dim xl As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet1 As Excel.Worksheet Dim xlpiv As PivotTable Dim objField As PivotField Dim RowField As PivotField Dim valueField As PivotField Set xl = CreateObject("Excel.Application") Set xlBook = xl.Workbooks.Open(OutPutFileName) Set xlSheet1 = xlBook.Worksheets(1) xlBook.Sheets("Qry_CustomsReport").Select Set xlpiv = xlSheet1.PivotTableWizard Set objField = xlpiv.PivotFields("NEA Plant Codes") objField.Orientation = xlRowField Set objField = xlpiv.PivotFields("Creation Date") objField.Orientation = xlColumnField Set RowField = objField Set objField = xlpiv.PivotFields("Creation Date") objField.Orientation = xlDataField objField.Function = xlCount Set valueField = objField RowField.PivotFilters.Add Type:=xlTopCount, DataField:=valueField, Value1:=10 xlpivC.ActiveSheet.Name = "PivotTable Plant" 

根据macroslogging器,试试这个语法:

 Set pivTable = PivotTables("YourPivotTable") 'The field you want the filter: Set rowField = pivTable.PivotFields("TheRowField") 'The aggregation you to use to determine the top 10: Set valueField = pivTable.PivotFields("Count of Creation Date") rowField.PivotFilters.Add2 Type:=xlTopCount, DataField:=valueField, Value1:=10 

幸运的是,现在这个问题已经解决了,感谢lowak和peter指导我正确的方向。 然而,我的macroslogging器提出了不同的代码行;

 xlSheet4pivCT.PivotFields("NEA Plant Codes").AutoShow _ xlAutomatic, xlTop, 10, "Count of Creation Date" 

看起来我在开始时尝试使用的代码是正确的,但是引用错误。 谢谢你们展示这个macros录像机,以后肯定会派上用场。