1

如果选择了切片器中的某个值,有什么方法可以隐藏某些行?我有一些图,只有在选择了一个特定链时才需要显示,如果没有选择 - 然后隐藏图(位于第 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
4

2 回答 2

1

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

地点:

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

如下所示:

笔记:

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

    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
于 2018-04-05T20:01:52.640 回答
0

要求:根据 a 的状态显示\隐藏 aRange行。SelectedSlicerItem

VBA 程序:( 根据 OP 方法)

尝试以下过程(请参阅过程中的注释\解释)

    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

另一种方法:

请记住, aSlicer是一种PivotTables双向工作的远程控制,即Slicer更新PivotTablesPivotTables更新Slicer,因此无需验证是否PivotTableSlicer操作更新,只需检查Visible属性目标PivotItem中的目标PivotTable,无论 和 是否Slicer连接PivotTable

此过程仅使用目标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
于 2018-04-06T01:32:59.427 回答