Hiding columns when other columns become visible

Godot60

Board Regular
Joined
May 11, 2017
Messages
62
I have written some code to make some columns visible based on a click of a button. I'd like to make it so when these columns appear (L & M) three other columns are hidden (I, J, and K). Any suggestions would be welcome. Thanks!

Sub Hide()
Sheets("Increase Program Workbook").Unprotect Password:="Password"
With Columns("L:M")
If .EntireColumn.Hidden = True Then
.EntireColumn.Hidden = False
Else
.EntireColumn.Hidden = True
End If
Sheets("Increase Program Workbook").Protect Password:="Password"
End With


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
Code:
Sub Hide()
   With Sheets("Increase Program Workbook")
      .Unprotect Password:="Password"
      With .Columns("L:M")
         .EntireColumn.Hidden = Not .EntireColumn.Hidden
         .Columns("I:K").EntireColumn.Hidden = Not .EntireColumn.Hidden
      End With
      .Protect Password:="Password"
   End With
End Sub
 
Upvote 0
I wonder if Fluff's code is trying to do too much at once. Maybe this modification will work:
Code:
Sub Hide()   Dim hc As Boolean
   With Sheets("Increase Program Workbook")
      .Unprotect Password:="Password"
      With .Columns("L:M")
         hc = .EntireColumn.Hidden
         .EntireColumn.Hidden = Not hc
         Columns("I:K").EntireColumn.Hidden = hc
      End With
      .Protect Password:="Password"
   End With
End Sub
 
Last edited:
Upvote 0
Actually, Fluff's original code will work with one minor edit.
Change this line:
Code:
.Columns("I:K").EntireColumn.Hidden = Not .EntireColumn.Hidden
to this:
Code:
Columns("I:K").EntireColumn.Hidden = Not .EntireColumn.Hidden
That leading period makes all the difference, between columns I:K are not contained within the range L:M.
 
Upvote 0
Good spot Joe
@Godot60
You may find that cols T:V are hidden, due to my mistake of putting the period before Columns("I:K")
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,555
Members
449,735
Latest member
Gary_M

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