Button that hides and unhides columns

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

In my table "ItemImport" I have the need sometimes to hide a few columns. To hide these with VBA is not that hard, but if you want to do that with one button then its a different story. Would this be possible and so that the button changes its text also. So from Hide to Unhide as text. The below action is what I need.

Thank you in advance and have a great day.

VBA Code:
Columns("Q:AT").Select
    Selection.EntireColumn.Hidden = True
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
What type of button do you have on your sheet?
There are several types of buttons and the operation for each of them is different.

🧙‍♂️
I give you 3 codes for the following buttons: ActiveX control CommandButton, ActiveX control togglebutton and a Shape.

VBA Code:
Private Sub CommandButton1_Click()
  With CommandButton1
    If .Caption = "Show" Then .Caption = "Hide" Else .Caption = "Show"
    Columns("Q:AT").EntireColumn.Hidden = .Caption = "Show"
  End With
End Sub

VBA Code:
Private Sub ToggleButton1_Click()
  With ToggleButton1
    If .Caption = "Show" Then .Caption = "Hide" Else .Caption = "Show"
    Columns("Q:AT").EntireColumn.Hidden = .Caption = "Show"
  End With
End Sub

In the following code, adjust the name of your shape on this line: ActiveSheet.Shapes("Button1").TextFrame2.TextRange.Characters
VBA Code:
Sub show_and_hide_columns()
  With ActiveSheet.Shapes("Button1").TextFrame2.TextRange.Characters
    If .Text = "Show" Then .Text = "Hide" Else .Text = "Show"
    Columns("Q:AT").EntireColumn.Hidden = .Text = "Show"
  End With
End Sub

1702908694212.png

I hope to hear from you very soon
Regards
Dante Amor
😇
 
Last edited:
Upvote 0
Solution
This is awesome, thank you for your efforts. I used the active X button in this case.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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