我创建了一个数据透视表,其中包含自上次被视作该列以来的天数。我想将过滤器添加到列标签中,以仅显示6天或更长时间未见过的项目。当我运行以下代码时,它无法正确过滤。自从上次出现以来,它会筛选显示6、8和9天,但是我需要的所有内容都大于6。我知道还有更多的项目要显示,因此这不是源数据问题。除过滤列标签外,其他所有操作均有效。任何帮助表示赞赏。
Sub OBPivotTable()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("train-details-outbound")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Range("A1").CurrentRegion
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="OB Dwell")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="OB Dwell")
'Insert Row Fields
With ActiveSheet.PivotTables("OB Dwell").PivotFields("CLM Status")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("OB Dwell").PivotFields("Location")
.Orientation = xlRowField
.Position = 2
End With
'Insert Data Field
With ActiveSheet.PivotTables("OB Dwell").PivotFields("Container #")
.Orientation = xlDataField
.Name = "OB Dwell"
End With
'Insert Column Fields and add label filter for >=6 days
With ActiveSheet.PivotTables("OB Dwell").PivotFields("Last Sighted Days")
.Orientation = xlColumnField
.Position = 1
End With
'Format Pivot Table
ActiveSheet.PivotTables("OB Dwell").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("OB Dwell").TableStyle2 = "PivotStyleMedium9"
'Adjust column C so it's not bigger than other columns
Columns("C:C").ColumnWidth = 2.5
'Filter columns to show items last seen 6 or more days ago
With ActiveSheet.PivotTables("OB Dwell").PivotFields("Last Sighted Days")
.PivotFilters.Add Type:=xlCaptionIsGreaterThanOrEqualTo, Value1:="6"
End With
End Sub