【发布时间】:2019-07-10 23:11:52
【问题描述】:
我正在尝试在我的数据透视表上选择一个过滤器,但我不知道为什么它不能正确读取我的代码。
它说“无法获取 PivotField 类的 PivotItems 属性”
Sub Filter_MasterCode()
Dim pt As PivotTable
Dim pf As PivotField
Dim sField As String
Set pt = ActiveSheet.PivotTables("PivotTable3")
sField = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Text
For Each pf In pt.PageFields
If pf.Name <> "Values" Then
pf.Orientation = xlHidden
End If
Next pf
For Each pf In pt.DataFields
If pf.Name <> "Values" Then
pf.Orientation = xlHidden
End If
Next pf
pt.PivotFields(sField).Orientation = xlPageField
With ActiveSheet.PivotTables("PivotTable3").PivotFields(sField)
.PivotItems("0").Visible = False
.PivotItems("1").Visible = True
End With
Select Case sField
Case Is = "Apple"
With ActiveSheet.PivotTables("PivotTable3").PivotFields("2017")
.Orientation = xlDataField
End With
End Select
.PivotItems("0").Visible = False 处显示错误
谢谢
【问题讨论】:
-
您可以添加数据透视表的样本吗?我猜
.PivotItems("0")不是一个有效的参考。如果您尝试根据第一项执行此操作,则索引从 1 开始,而不是 0。