Excel 使用切片器隐藏/取消隐藏列

Excel 使用切片器隐藏/取消隐藏列,excel,vba,pivot-table,slicers,Excel,Vba,Pivot Table,Slicers,我有两张excel表格。第一种称为“报告”,第二种称为“表格”。在“报告”页面上,我有一个名为“PivotTable1”的数据透视表,其活动字段名为“值”,并且我的值/列位于数据透视表的过滤器区域。我还想在B84:AC123中显示表。位于G83:AC83中的表格上方,我复制了列标题以用作宏中的筛选器(名称框为“rngValues”) 在第二页(“表格”)中,我的值/表格列标题为A2:A25,A2为标题(称为“值”)。表名为“tblValues” 目标是在“报表页面”中使用数据透视表1中的切片器来

我有两张excel表格。第一种称为“报告”,第二种称为“表格”。在“报告”页面上,我有一个名为“PivotTable1”的数据透视表,其活动字段名为“值”,并且我的值/列位于数据透视表的过滤器区域。我还想在B84:AC123中显示表。位于G83:AC83中的表格上方,我复制了列标题以用作宏中的筛选器(名称框为“rngValues”)

在第二页(“表格”)中,我的值/表格列标题为A2:A25,A2为标题(称为“值”)。表名为“tblValues”

目标是在“报表页面”中使用数据透视表1中的切片器来隐藏/取消隐藏列。为什么下面的代码不起作用

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

'Run the filter columns macro when the pivot table or slicer is changed  
  
    Select Case Target.Name
        Case "PivotTable1"
            Call m_FilterCol.Filter_Columns("rngValues", "Report", "Report", "PivotTable1", "Values")
    End Select
End Sub

Sub Filter_Columns(sHeaderRange As String, _
                    sReportSheet As String, _
                    sPivotSheet As String, _
                    sPivotName As String, _
                    sPivotField As String _
                    )

Dim c As Range
Dim rCol As Range
Dim pi As PivotItem
 
    
    'Unhide all columns
    Worksheets(sReportSheet).Range(sHeaderRange).EntireColumn.Hidden = False
    
    'Loop through each cell in the header range and compare to the selected filter item(s).
    'Hide columns that are not selected/filtered out.
    
    For Each c In Worksheets(sReportSheet).Range(sHeaderRange).Cells
        
        'Check if the pivotitem exists
        With Worksheets(sPivotSheet).PivotTables(sPivotName).PivotFields(sPivotField)
            On Error Resume Next
            Set pi = .PivotItems(c.Value)
            On Error GoTo 0
        End With
            
        'If the pivotitem exists then check if it is visible (filtered)
        If Not pi Is Nothing Then
            If pi.Visible = False Then
            
                'Add excluded items to the range to be hidden
                If rCol Is Nothing Then
                    Set rCol = c
                Else
                    Set rCol = Union(rCol, c)
                End If
            End If
        End If
        
        'Reset the pivotitem
        Set pi = Nothing
        
    Next c
    
    'Hide the columns of the range of excluded pivot items
    If Not rCol Is Nothing Then
        rCol.EntireColumn.Hidden = True
    End If
    
End Sub