微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用切片器隐藏/取消隐藏列

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

在第二张表(“表”)上,我在A2:A25中具有值/表列标题,其中A2为标题(称为“值”)。表名是“ tblValues”。

目标是使用“报告页面”中Pivottable1中的切片器来隐藏/取消隐藏列。为什么下面的这段代码不起作用?

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","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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。