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

问题描述

我在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 (将#修改为@)