通过VBA进行数据透视表过滤。 页面过滤器返回错误。我想这是由于格式-这些项目是日期-。如何正确应用过滤器?谢谢!
Sub FilterPivotTable()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set pt = Sheets("pivotTable").PivotTables("PivotTable1") ' PivotTable1 is the name of the table. Fix variable
Dim pItem As String ' the value from "H3" is filtered
pItem = Sheets("pivotTable").range("H3")
' Pivot Table Fields: KG Date Units Value
Set pf = pt.PivotFields("Date")
pf.ClearAllFilters
' pi is returning an error due to the cell format
' cell format is Date
Set pi = pf.PivotItems(pItem)
pi.Visible = False
Debug.Print pi
End Sub