我曾尝试从互联网永远复制和粘贴解决方案,现在尝试使用VBA在Excel中过滤数据透视表。下面的代码不起作用。使用VBA过滤Excel数据透视表
Sub FilterPivotTable()
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable2").ManualUpdate = True
ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").CurrentPage = "K123223"
ActiveSheet.PivotTables("PivotTable2").ManualUpdate = False
Application.ScreenUpdating = True
End Sub
我想过滤所以我看到所有具有SavedFamilyCode K123223的行。我不想在数据透视表中看到任何其他行。无论以前的过滤器如何,我都希望这可以工作。我希望你能帮助我。谢谢!根据您的文章
我想:
Sub FilterPivotField()
Dim Field As PivotField
Field = ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode")
Value = Range("$A$2")
Application.ScreenUpdating = False
With Field
If .Orientation = xlPageField Then
.CurrentPage = Value
ElseIf .Orientation = xlRowField Or .Orientation = xlColumnField Then
Dim i As Long
On Error Resume Next ' Needed to avoid getting errors when manipulating fields that were deleted from the data source.
' Set first item to Visible to avoid getting no visible items while working
.PivotItems(1).Visible = True
For i = 2 To Field.PivotItems.Count
If .PivotItems(i).Name = Value Then _
.PivotItems(i).Visible = True Else _
.PivotItems(i).Visible = False
Next i
If .PivotItems(1).Name = Value Then _
.PivotItems(1).Visible = True Else _
.PivotItems(1).Visible = False
End If
End With
Application.ScreenUpdating = True
End Sub
不幸的是我得到运行时错误91:对象没有设置变量或With块变量。是什么导致了这个错误?
您是否尝试录制宏? –
我相处 子FilterPivotTable() 线的东西随着ActiveSheet.PivotTables( “PivotTable2”)。透视字段( “SavedFamilyCode”) .PivotItems( “K010”)。可见=真 .PivotItems( “K012” )。可见=假 尾随着 结束小组 但我想所有除K010成为无形 宏录制忽略我的选择/取消所有点击 – user1283776