【问题标题】:VBA: How to hide filtered (Spliced) Columns?VBA:如何隐藏过滤(拼接)列?
【发布时间】:2017-08-21 10:24:05
【问题描述】:

我正在尝试实现一个 VBA 代码,该代码隐藏所选过滤器“不必要”的列。

我正在使用切片器来简化对我的表的过滤,但每个子集都有不同的信息(数据条目)。

例如。过滤类型 A 的数据在 E 和 F 列,而类型 B 的数据在 E 和 G 等。

现在我希望在任何给定时刻只显示其中包含数据的列,以使数据集更加用户友好。

我目前正在处理此代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Long
    Dim n As Long
    Application.ScreenUpdating = False
    n = Rows(2).Find(What:="*", SearchDirection:=xlPrevious).Column
    For c = 3 To n
        Cells(3, c).EntireColumn.Hidden = (Application.CountA(Columns(c)) < 2)
    Next c
    Application.ScreenUpdating = True
End Sub

您可能注意到,这不会从切片器中隐藏基于“活动过滤器”的列,也不会在更改过滤器时触发(例如,在切片器中选择 B 类而不是 A 类)

【问题讨论】:

    标签: vba excel filter slicers


    【解决方案1】:
    Sub hideempty()
    Dim i As Long
    Dim LastCol As Long
    Dim lastrow as long
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column 'If it has a fixed number of columns, _
                                                            you can replace this to LastCol = 5, for example. _
                                                            The number 1 is the row where the headers are
    For i = 1 To LastCol
        lastrow = Cells(Rows.Count, i).End(xlUp).Row
        If lastrow <= 1 Then '1 considering every column has a header, regardless of having any data. _
                                If there are no headers, replace with 0
            Columns(i).EntireColumn.Hidden = True
        Else 'this will show columns that were previously hidden if now they have data
            Columns(i).EntireColumn.Hidden = False
        End If
    Next i
    End Sub
    

    【讨论】:

    • 谢谢,这似乎有效!但是,我仍在为切片器选择的持续更新而苦苦挣扎。例如。根据切片器中的选择,打开一些,隐藏一些列!
    • 对不起,我想我误读了你的问题。我完全对切片机一无所知。我看看能不能做得更好
    • 谢谢,我似乎找不到通过切片器触发/执行代码的任何方法,尤其是在常规表而不是数据透视表上实现时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多