Hide/Unhide toggle button which changes colour and text in vba

Arrow081974

New Member
Joined
Aug 4, 2018
Messages
3
Hello

I've been trying to write some VBA to set up a toggle button to hide multiple predefined columns.

I initially managed to define the code for the hide/unhide toggle and attached this to a shape

Sub togglecols()


Range("e:e,g:g").EntireColumn.Hidden = Not Range("e:e,g:g").EntireColumn.Hidden


End Sub

I then wanted a toggle button which would then change the colour of the button and text displayed on the button

Private Sub CommandButton1_Click()
With CommandButton1
If .BackColor = vbGreen Then
.BackColor = vbRed
.Caption = "Hidden"
.ForeColor = vbWhite
Else
.BackColor = vbGreen
.Caption = "Unhidden"
.ForeColor = vbBlack
End If
End With
End Sub

What I'm struggling with is combining these into a single piece of code that does both of these actions.

Broadly speaking what I want is
- when columns are hidden I want the toggle button to say "Hidden" and the toggle button to become red with white text
- when columns are not hidden I want the toggle button to say "Not Hidden" and the toggle button to become green with black text

Is anybody able to point me in the right direction? Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try
Code:
Sub togglecols()

    With Range("e:e,g:g").EntireColumn
        .Hidden = Not(.Hidden)
        if .Hidden then
            CommandButton1.Caption = "UnHide"
        Else
           CommandButton1.Caption = "Hide"
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Another option
Code:
Private Sub CommandButton1_Click()
      Range("E:E,G:G").EntireColumn.Hidden = Not Range("E:E,G:G").EntireColumn.Hidden
      With CommandButton1
         .BackColor = IIf(.BackColor = vbRed, vbGreen, vbRed)
         .Caption = IIf(.Caption = "Hidden", "Unhidden", "Hidden")
         .ForeColor = IIf(.ForeColor = vbWhite, vbBlack, vbWhite)
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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