在vba中PivotField对象代表数据透视表的字段。
数据透视表的行、列、数值、筛选(页)字段均可以用PivotField对象表示。
所有的字段项目可以使用PivotItem对象表示。
要对数据透视表的页字段(筛选字段)进行筛选,先使用ClearAllFilters方法清除原字段的选项,然后可以直接使用CurrentPage属性设置要显示的筛选值即可。
以下vba代码举例演示了如何快速地对数据透视表中的页字段(筛选字段)进行筛选:
Sub QQ1722187970()
Dim oPC As PivotCache
Dim oPT As PivotTable
Dim oPF As PivotField
Dim oPI As PivotItem
Dim oWK As Worksheet
Set oWK = Sheet2
Set oPT = oWK.PivotTables(1)
With oPT
Set oPF = .PivotFields("分类")
With oPF
'先清除所有项目的筛选
.ClearAllFilters
'重新筛选
.CurrentPage = "新标"
End With
End With
End Sub
以上是针对数据透视表的页字段(筛选字段)的单个项目的筛选,如果要同时筛选多个项目,则需要先设置EnableMultiplePageItems为True,然后将不要显示的字段项目PivotItem对象的Visible属性设置为False才行,代码如下:
Sub QQ1722187970()
Dim oPC As PivotCache
Dim oPT As PivotTable
Dim oPF As PivotField
Dim oPI As PivotItem
Dim oWK As Worksheet
Set oWK = Sheet2
Set oPT = oWK.PivotTables(1)
With oPT
Set oPF = .PivotFields("分类")
With oPF
.EnableMultiplePageItems = True
'把不要显示的字段项目设置为不可见
.PivotItems("新标").Visible = False
.PivotItems("整体标").Visible = False
End With
End With
End Sub


qnn7k1