how to hide/unhide columns using buttons?

Viewed 213

I created multiple toggle buttons to hide/show columns, to get monthly revenue. What I need is when the user presses any two or more buttons, for example, if the January and March buttons are pressed, so only the (B:F) and (N:R) columns should be displayed and the reset columns are hidden. Basically it's like filtering by slicer, in other words, no matter how much the user presses, they should be able to see the columns for those specific months at the beginning of the page and the reset is hidden.

The Problem: What toggle buttons do is just hide/show certain columns accordingly I need also the user can see only columns of what he pressed.

please find the link for the excel sheet: https://1drv.ms/x/s!Av2jQlwHZCT3gj7BPSjUvAnWbXgs?e=XuKB6T

2 Answers

I had a go at doing it as it was a little interesting to me. Place all this code into your sheet module:

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

Things to note:

  • I based it on your project (I downloaded it) so everything should be correct.
  • ColRng is the column list for each month in order of Jan to Dec.
  • Remember that because no sheet is specified, this will do the changes on the active sheet.
  • The number in the click events (e.g HideColumns (1)) is the month number. I see you have the buttons arranged in order so ToggleButton1 equals January.

I had togglecount saved to the range but changed so that isn't needed and it just checks what columns are hidden already before continuing. That way the code is self-reliant.

EDIT: I've updated the code to a different way. This new method is a lot simpler and just loops through the toggle buttons themselves. It hides all columns then loops through the buttons checking if they are toggled or not.

Toggle Hidden Columns

  • In a nutshell, hide all then show the 'chosen' (If dict(n) Then, short for If dict(n).Value Then, short for If dict(n).Value = True Then, think e.g. If Sheet1.ToggleButton1 Then, short for If Sheet1.ToggleButton1.Value Then, short for If Sheet1.ToggleButton1.Value = True Then).

Standard Module e.g. 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 Module e.g. 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
Related