我正在尝试通过vba过滤前10行的数据透视表。图中描绘了手动方式,以明确我的意思。
到目前为止,我已经尝试了以下内容,但我无法让它工作。 (ObjField在这里指的是创建日期的计数。)
objField.AutoShow Type:=xlAutomatic, Range:=xlTop, Count:=10, Field:="Count of Creation Date"
我确信objfield引用本身是正确的,因为通过代码对pivottable的其他调整工作得很好。但是,我可能在这里使用错误的枢轴场。任何有关这个小问题的帮助都将不胜感激!
修改
完整代码;从访问调用:
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"
答案 0 :(得分:2)
根据宏录制器,请尝试以下语法:
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
答案 1 :(得分:1)
幸运的是,这个问题现在已经解决了,感谢lowak和peter让我指向正确的方向。然而,我的宏录制器提出了不同的代码行;
xlSheet4pivCT.PivotFields("NEA Plant Codes").AutoShow _
xlAutomatic, xlTop, 10, "Count of Creation Date"
我试图在开始时使用的代码似乎是正确的,但是错误的引用。谢谢大家展示这款宏录音机,以后肯定会派上用场。