1

我有一个切片器,其中包含数据透视表中的项目。我的用户希望能够隐藏某些切片器项目,即使这些项目包含数据。有没有办法通过 VBA 代码隐藏切片项?

4

1 回答 1

0

为什么不直接过滤掉它们呢?如果这是 OLAP PivotTable,请参阅Pivot Table filter out only 1 选项

如果这是一个“传统”数据透视表,那么您可以使用我在Pivotfields 多重过滤器上发布的代码的变体

这是一些修改后的代码:

Sub FilterSlicer_Inverse()
Dim slr As Slicer
Dim sc As SlicerCache
Dim si As SlicerItem
Dim i As Long
Dim vItem As Variant
Dim vSelection As Variant
Dim pt As PivotTable

Set sc = ActiveWorkbook.SlicerCaches("Slicer_test")
vSelection = Array("1", "2") <= List the items you want to hide in here
For Each pt In sc.PivotTables
    pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed
Next pt

With sc
    .ClearAllFilters
    On Error Resume Next 'In case one of the items isn't found
    For Each vItem In vSelection
        .SlicerItems(vItem).Selected = False
    Next vItem
    On Error GoTo 0
End With

For Each pt In sc.PivotTables
    pt.ManualUpdate = False
Next pt

End Sub

请注意,如果您更改 PivotItems 或 SlicerItems 并不重要......您会得到相同的结果。

于 2018-03-28T09:39:28.717 回答