ToggleButton hide columns under conditions

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
Hi all,

I have the code below now, but when I manually hide a column (for example column C) and use the toggle button twice, column C is visible again and I want it to stay hidden. Is there a possibility to do that?


Code:
Private Sub ToggleButton1_Click()
Dim xAddress As String
xAddress = "A:Z"
If ToggleButton1.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Another option:-
Sorry about the previous !!!

Code:
Option Explicit
[COLOR="Navy"]Dim[/COLOR] rng [COLOR="Navy"]As[/COLOR] Range
Private [COLOR="Navy"]Sub[/COLOR] ToggleButton1_Click()
[COLOR="Navy"]Dim[/COLOR] xAddress [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] Range
Static Fd [COLOR="Navy"]As[/COLOR] Boolean
xAddress = "A:Z"
Fd = Fd Xor True
 
 [COLOR="Navy"]If[/COLOR] Fd [COLOR="Navy"]Then[/COLOR]
 [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ac [COLOR="Navy"]In[/COLOR] Range("A1:Z1")
    [COLOR="Navy"]If[/COLOR] Ac.Columns.Hidden [COLOR="Navy"]Then[/COLOR]
       [COLOR="Navy"]If[/COLOR] rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] rng = Ac Else [COLOR="Navy"]Set[/COLOR] rng = Union(rng, Ac)
    [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] If
 
[COLOR="Navy"]If[/COLOR] ToggleButton1.Value [COLOR="Navy"]Then[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = True
[COLOR="Navy"]Else[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = False
[COLOR="Navy"]End[/COLOR] If

[COLOR="Navy"]If[/COLOR] Not Fd [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        rng.EntireColumn.Hidden = True
        [COLOR="Navy"]Set[/COLOR] rng = Nothing
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Perhaps
Code:
Private Sub ToggleButton1_Click()

Static StayHidden() As Boolean
Dim xRange As Range
Dim i As Long

Set xRange = Range("A:G")

On Error Resume Next
If UBound(StayHidden) < 1 Then
    ReDim StayHidden(1 To xRange.Columns.Count)
End If
On Error GoTo 0

If ToggleButton1.Value Then
    For i = 1 To xRange.Columns.Count
        StayHidden(i) = xRange.Columns(i).Hidden
    Next i
    Rem hide all
    xRange.EntireColumn.Hidden = True
Else
    For i = 1 To xRange.Columns.Count
        xRange.Columns(i).Hidden = StayHidden(i)
    Next i
End If

End Sub

If I hide column G. This code only hides till column G, not column H to Z.
 
Upvote 0
Another option:-
Sorry about the previous !!!

Code:
Option Explicit
[COLOR=Navy]Dim[/COLOR] rng [COLOR=Navy]As[/COLOR] Range
Private [COLOR=Navy]Sub[/COLOR] ToggleButton1_Click()
[COLOR=Navy]Dim[/COLOR] xAddress [COLOR=Navy]As[/COLOR] [COLOR=Navy]String,[/COLOR] Ac [COLOR=Navy]As[/COLOR] Range
Static Fd [COLOR=Navy]As[/COLOR] Boolean
xAddress = "A:Z"
Fd = Fd Xor True
 
 [COLOR=Navy]If[/COLOR] Fd [COLOR=Navy]Then[/COLOR]
 [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Ac [COLOR=Navy]In[/COLOR] Range("A1:Z1")
    [COLOR=Navy]If[/COLOR] Ac.Columns.Hidden [COLOR=Navy]Then[/COLOR]
       [COLOR=Navy]If[/COLOR] rng [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR] [COLOR=Navy]Set[/COLOR] rng = Ac Else [COLOR=Navy]Set[/COLOR] rng = Union(rng, Ac)
    [COLOR=Navy]End[/COLOR] If
 [COLOR=Navy]Next[/COLOR] Ac
[COLOR=Navy]End[/COLOR] If
 
[COLOR=Navy]If[/COLOR] ToggleButton1.Value [COLOR=Navy]Then[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = True
[COLOR=Navy]Else[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = False
[COLOR=Navy]End[/COLOR] If

[COLOR=Navy]If[/COLOR] Not Fd [COLOR=Navy]Then[/COLOR]
    [COLOR=Navy]If[/COLOR] Not rng [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR]
        rng.EntireColumn.Hidden = True
        [COLOR=Navy]Set[/COLOR] rng = Nothing
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

This one works, thanks!

Why do you have to place the "Option Explicit" & "Dim rng As Range" before the macro?
This makes it not clear when I have multiple macros on one sheet.
 
Upvote 0
You're welcome
You can also do it like this:-
Code:
Private [COLOR="Navy"]Sub[/COLOR] ToggleButton1_Click()
[COLOR="Navy"]Dim[/COLOR] xAddress [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] Range
Static rng [COLOR="Navy"]As[/COLOR] Range
Static Fd [COLOR="Navy"]As[/COLOR] Boolean
xAddress = "A:Z"
Fd = Fd Xor True
 
 [COLOR="Navy"]If[/COLOR] Fd [COLOR="Navy"]Then[/COLOR]
 [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Ac [COLOR="Navy"]In[/COLOR] Range("A1:Z1")
    [COLOR="Navy"]If[/COLOR] Ac.Columns.Hidden [COLOR="Navy"]Then[/COLOR]
       [COLOR="Navy"]If[/COLOR] rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] rng = Ac Else [COLOR="Navy"]Set[/COLOR] rng = Union(rng, Ac)
    [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] If
 
[COLOR="Navy"]If[/COLOR] ToggleButton1.Value [COLOR="Navy"]Then[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = True
[COLOR="Navy"]Else[/COLOR]
    Application.ActiveSheet.Columns(xAddress).Hidden = False
[COLOR="Navy"]End[/COLOR] If

[COLOR="Navy"]If[/COLOR] Not Fd [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        rng.EntireColumn.Hidden = True
        [COLOR="Navy"]Set[/COLOR] rng = Nothing
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top