【问题标题】:VBA to hide SlicerItemsVBA 隐藏 SlicerItems
【发布时间】:2018-03-22 11:28:38
【问题描述】:

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

【问题讨论】:

    标签: show-hide slicers


    【解决方案1】:

    为什么不直接过滤掉它们呢?如果这是一个 OLAP 数据透视表,请参阅Pivot Table filter out only 1 option

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

    以下是一些修改后的代码:

    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 都没有关系...您会得到相同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多