VBA control button to Toggle hide and unhide columns

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
370
Hi, I have a form control button called "Hide Col" that runs the code below. I would like to make the button toggle to hide and unhide. I tried to implement some code that I searched online but I'm unable to do it correctly. Also, I see it's possible to have the control button name change as well. So could we have it say "Hide Col" and then ""show Col" ? Thanks in advance

Code:
Sub DETAIL_FORM_HIDE_COL()
Dim c As Range
    For Each c In Range("I12:Z12").Cells
        If c.Value = "HIDE" Then
            c.EntireColumn.Hidden = True
    End If
    Next c
End Sub
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
have you tried using the GROUP function which gives you a button and no code. IN the Page Layout
 
Upvote 0
One way. The button you create must be edited so that the label is initially "Hide Col"


Code:
Sub DETAIL_FORM_HIDE_COL()
    Dim c As Range
    Dim Sh As Shape

    For Each Sh In ActiveSheet.Shapes
        With Sh
            If .Type = 8 And (Sh.TextFrame.Characters.Text = "Hide Col" Or Sh.TextFrame.Characters.Text = "Show Col") Then
                Exit For
            End If
        End With
    Next Sh

    If Not Sh Is Nothing Then
        For Each c In Range("I12:Z12").Cells
            If c.Value = "HIDE" Then
                If Not c.EntireColumn.Hidden Then
                    c.EntireColumn.Hidden = True
                    Sh.TextFrame.Characters.Text = "Show Col"
                Else
                    c.EntireColumn.Hidden = False
                    Sh.TextFrame.Characters.Text = "Hide Col"
                End If
            End If
        Next c
    End If
End Sub
 
Upvote 0
Yes - best to start a new post for a different topic
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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