根据切片器选择隐藏 sheet 行

hide sheet rows based on slicer selection

如果选择了切片器中的某个值,是否有任何方法可以隐藏某些行? 我有一些图只有在选择了一个特定的链时才需要显示,如果没有选择 - 然后隐藏图(位于 287:345 行)。 我试过以下,但没用:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If ActiveWorkbook.SlicerCaches("Slicer_Chain").SlicerItems("ChainName").Selected = True Then
        Rows("287:346").Hidden = False
    Else
        Rows("287:346").Hidden = True
    End If

End Sub

我会针对与切片器关联的数据透视表的 Worksheet_PivotTableUpdate 事件。

地点:

在作品的代码窗格中sheet 包含与切片器关联的数据透视表。

类似如下:

注:

  1. 适当更改数据透视表名称
  2. 如果要隐藏的行在不同的 sheet 中,请在行前添加工作sheet 名称,例如

    ThisWorkbook.Worksheets("Sheet1").Rows("287:346").EntireRow.Hidden = False
    

代码:

Option Explicit

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

    If Target.Name <> "PivotTable1" Then
        Exit Sub
    Else
       If Parent.SlicerCaches("Slicer_Chain").SlicerItems("ChainName").Selected Then
        Rows("287:346").EntireRow.Hidden = False
       Else
        Rows("287:346").EntireRow.Hidden = True
       End If
    End If

End Sub

要求: 根据 SlicerItemSelected 状态 show\hide 行 Range

VBA 过程: (根据 OP 方法)

尝试以下过程(参见过程中的 comments\explanation):

    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Dim sc As SlicerCache, sl As Slicer
    Dim sPt As String, sFld As String, sItm As String, sRng As String

    'Use variables to hold the criteria to be applied
        sPt = "PivotTable1"
        sFld = "Chain"
        sItm = "A.6"
        sRng = "287:346"

        With Target

            Rem Validate PivotTable
            If .Name <> sPt Then Exit Sub

    ' As Slicer names can be easily changed by users, need to identify
    ' the SlicerCache connected to the target `PivotTable` using the 
    ' SourceName of the PivotField. This step returns the SlicerCache 
    ' connected to the PivotTable otherwise, the SlicerCache is nothing.
            Rem Set SlicerCache
            For Each sl In .Slicers
                If sl.SlicerCache.SourceName = sFld Then
                    Set sc = sl.SlicerCache
                    Exit For
        End If: Next: End With

        Rem Validate SlicerItem & Apply Result
        If Not (sc Is Nothing) Then

    ' This line Shows\Hides the target range based on the opposite 
    ' status of the target SlicerItem.
            Me.Rows(sRng).EntireRow.Hidden = Not (sc.SlicerItems(sItm).Selected)

        Else

    ' PivotTable is not connected to a Slicer of the target PivotField
            MsgBox "PivotTable [" & sPt & "]" & String(2, vbLf) & _
                vbTab & "is not connected to Slicer of Field [" & sFld & "].", _
                vbApplicationModal + vbInformation, "Slicers Selection"

        End If

        End Sub

另一种方法:

请记住,Slicer 是一种双向工作的 PivotTables 遥控器,即 Slicer 更新 PivotTablesPivotTables 更新 Slicer,因此无需验证 PivotTable 是否由 Slicer 操作更新,只需要检查 Visible 属性 的目标 PivotItem 在目标 PivotTable 中,无论 SlicerPivotTable 是否连接。

此过程仅使用目标 PivotTable,即根据 PivotItem 在数据透视表中是否可见显示或隐藏目标范围。

    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Dim sPt As String, sFld As String, sItm As String, sRng As String

        sPt = "PivotTable1"
        sFld = "Chain"
        sItm = "A.6"
        sRng = "287:346"

        With Target

            Rem Validate PivotTable
            If .Name <> sPt Then Exit Sub

            Rem Validate PivotItem & Apply Result
            With .PivotFields(sFld).PivotItems(sItm)
                Me.Rows(sRng).EntireRow.Hidden = Not (.Visible)

        End With: End With

        End Sub