如何使用按钮 hide/unhide 列?

how to hide/unhide columns using buttons?

我为 hide/show 列创建了多个切换按钮,以获得每月收入。我需要的是当用户按下任意两个或更多按钮时,例如,如果按下 January 和 March 按钮,则只显示 (B:F) 和 (N:R) 列,隐藏重置列.基本上这就像切片器过滤,换句话说,无论用户按多少,他们都应该能够在页面开头看到那些特定月份的列,并且重置是隐藏的。

问题:切换按钮的作用只是hide/show相应的某些列我还需要用户只能看到他按下的列。

请为 excel sheet 找到 link: https://1drv.ms/x/s!Av2jQlwHZCT3gj7BPSjUvAnWbXgs?e=XuKB6T

我试了一下,因为它对我来说有点有趣。将所有这些代码放入您的 sheet 模块:

Private Sub ToggleButton1_Click()
HideColumns (1)
End Sub

Private Sub ToggleButton2_Click()
HideColumns (2)
End Sub

Private Sub ToggleButton3_Click()
HideColumns (3)
End Sub

Private Sub ToggleButton4_Click()
HideColumns (4)
End Sub

Private Sub ToggleButton5_Click()
HideColumns (5)
End Sub

Private Sub ToggleButton6_Click()
HideColumns (6)
End Sub

Private Sub ToggleButton7_Click()
HideColumns (7)
End Sub

Private Sub ToggleButton8_Click()
HideColumns (8)
End Sub

Private Sub ToggleButton9_Click()
HideColumns (9)
End Sub

Private Sub ToggleButton10_Click()
HideColumns (10)
End Sub

Private Sub ToggleButton11_Click()
HideColumns (11)
End Sub

Private Sub ToggleButton12_Click()
HideColumns (12)
End Sub

Sub HideColumns(MonthID As Integer)

Dim ColRng As Variant, i As Long, ToggleCount As Long

ColRng = Array("B:G", "H:M", "N:S", "T:Y", "Z:AE", "AF:AK", "AL:AQ", "AR:AW", "AX:BC", "BD:BI", "BJ:BO", "BP:BU", "B:BU")

Columns(ColRng(12)).Hidden = True
Dim ctl As OLEObject
For Each ctl In Me.OLEObjects
    If Left(ctl.Name, 6) = "Toggle" Then
        i = Mid(ctl.Name, 13)
        If ctl.Object.Value = True Then
            Columns(ColRng(i - 1)).Hidden = False
            ToggleCount = ToggleCount + 1
        End If
    End If
Next

If ToggleCount = 0 Then
    Columns(ColRng(12)).Hidden = False
End If

End Sub

注意事项:

  • 我基于你的项目(我下载了它)所以一切都应该是正确的。
  • ColRng是按1月到12月顺序排列的每个月的列列表
  • 请记住,因为没有指定 sheet,这将对活动 sheet 进行更改。
  • 点击事件中的数字(例如HideColumns (1))是月份数字。我看到你按顺序排列了按钮,所以 ToggleButton1 等于一月。

我已将 togglecount 保存到该范围但已更改,因此不需要它,它只是在继续之前检查已经隐藏了哪些列。这样代码就可以自力更生了。

编辑:我已将代码更新为不同的方式。这种新方法要简单得多,只需循环切换按钮本身。它隐藏所有列然后遍历按钮检查它们是否被切换。

切换隐藏列

  • 简而言之,隐藏所有然后显示 'chosen'(If dict(n) ThenIf dict(n).Value Then 的缩写,If dict(n).Value = True Then 的缩写,例如 If Sheet1.ToggleButton1 ThenIf Sheet1.ToggleButton1.Value Then 的缩写,If Sheet1.ToggleButton1.Value = True Then 的缩写).

标准模块,例如Module1

Option Explicit

Sub ToggleColumns( _
        ByVal ws As Worksheet)
    
    Const fCols As String = "B:G"
    Const First As Long = 1
    Const Last As Long = 12
    Const Pattern As String = "ToggleButton"
    
    Dim dict As Object: Set dict = DictToggleButtons(ws, Pattern)
    If dict Is Nothing Then Exit Sub
    
    Dim frg As Range: Set frg = ws.Columns(fCols) ' First
    Dim cCount As Long: cCount = frg.Columns.Count
    Dim trg As Range: Set trg = frg.Resize(, Last * cCount) ' Total
    trg.Hidden = False
     
    Dim hrg As Range
    Dim n As Long
    For n = First To Last
        If dict(n) Then
            Set hrg = GetCombinedRange(hrg, frg.Offset(, (n - 1) * cCount))
        End If
    Next n
    If hrg Is Nothing Then Exit Sub
    
    trg.Hidden = True
    hrg.EntireColumn.Hidden = False
    
End Sub

Function DictToggleButtons( _
    ByVal ws As Worksheet, _
    Optional ByVal Pattern As String = "ToggleButton") _
As Object
    On Error GoTo ClearError
    
    Dim oleLen As Long: oleLen = Len(Pattern)
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    
    Dim ole As OLEObject
    Dim tbtn As ToggleButton
    Dim tName As String
    Dim n As Long
    
    For Each ole In ws.OLEObjects
        Set tbtn = Nothing
        On Error Resume Next
        Set tbtn = ole.Object
        On Error GoTo ClearError
        If Not tbtn Is Nothing Then
            tName = tbtn.Name
            If Left(tName, oleLen) = Pattern Then
                n = CLng(Right(tName, Len(tName) - oleLen))
                Set dict(n) = tbtn
            End If
        End If
    Next ole
    Set DictToggleButtons = dict

ProcExit:
    Exit Function
ClearError:
    Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
    Resume ProcExit
End Function

Function GetCombinedRange( _
    ByVal BuiltRange As Range, _
    ByVal AddRange As Range) _
As Range
    If BuiltRange Is Nothing Then
        Set GetCombinedRange = AddRange
    Else
        Set GetCombinedRange = Union(BuiltRange, AddRange)
    End If
End Function

Sheet 模块例如Sheet1(Monthly Revenue) (CodeName(TabName))

Option Explicit

Private Sub ToggleButton1_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton2_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton3_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton4_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton5_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton6_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton7_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton8_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton9_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton10_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton11_Click()
    ToggleColumns Me
End Sub

Private Sub ToggleButton12_Click()
    ToggleColumns Me
End Sub