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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try:-
Code:
Private Sub ToggleButton1_Click()
Dim xAddress As String, Hid As Boolean
xAddress = "A:Z"
 Hid = Columns("C:C").Hidden
If ToggleButton1.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
Columns("C:C").Hidden = IIf(Hid, True, False)
End Sub
 
Last edited:
Upvote 0
Try:-
Code:
Private Sub ToggleButton1_Click()
Dim xAddress As String, Hid As Boolean
xAddress = "A:Z"
 Hid = Columns("C:C").Hidden
If ToggleButton1.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
Columns("C:C").Hidden = IIf(Hid, True, False)
End Sub

Hi Mick,

This only works with the example of column C. But what if I hide column E instead?

Thanks for your help
 
Upvote 0
Try this for columns from "A-Z"
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] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] ray(1 To 26) [COLOR="Navy"]As[/COLOR] Boolean

xAddress = "A:Z"
 [COLOR="Navy"]For[/COLOR] ac = 1 To 26
    ray(ac) = Columns(ac).Hidden
 [COLOR="Navy"]Next[/COLOR] ac
 
[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"]For[/COLOR] ac = 1 To 26
    Columns(ac).Hidden = ray(ac)
 [COLOR="Navy"]Next[/COLOR] ac

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this for columns from "A-Z"
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] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] ray(1 To 26) [COLOR=Navy]As[/COLOR] Boolean

xAddress = "A:Z"
 [COLOR=Navy]For[/COLOR] ac = 1 To 26
    ray(ac) = Columns(ac).Hidden
 [COLOR=Navy]Next[/COLOR] ac
 
[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]For[/COLOR] ac = 1 To 26
    Columns(ac).Hidden = ray(ac)
 [COLOR=Navy]Next[/COLOR] ac

[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

I don't know what this macro does, but it doesn't work for me..

What if I put an "x" in every columns first row that doesn't need to be hidden or unhidden (in the range). Is it possible then?
 
Upvote 0
The code works by looping through columns "A-Z" then logging, in array Ray whether the column is previously hidden or not. The columns A-Z are then Hidden, The loop is run again and if the column was previously hidden it will hide it again.
What part of that, does the code not do when you run it.??
 
Upvote 0
Perhaps
Code:
Private 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
 
Upvote 0
Perhaps
Code:
Private 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

Hi Mike, I get an error on the "Static" at the beginning of your macro:
compile error. Invalid outside procedure
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

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