Macro to hide/unhide columns

rhaney

Board Regular
Joined
Sep 27, 2009
Messages
64
Good morning,

Does anyone have a macro that I can assign to a button on a page? When the button is pressed, it will hide columns D, F, H, J, L, N, P, R on that page. When the button is pressed again, it will unhide the same columns. If necessary, I could use two buttons, one per event, but I would prefer one button if possible.

Thank you!

Robert
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try

Code:
Sub HideColumns()
Dim MyRange As Range
Set MyRange = Range("D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R")
MyRange.EntireColumn.Hidden = Not MyRange.EntireColumn.Hidden
End Sub
 
Upvote 0
Hi jonmo1,

I have a similar problem with rhaney in that I also wanted to hide columns. In my case, I wanted to hide all columns in Sheet1 except columns A:G. I managed to learn how to hide selected columns with vba but I wanted to add a password to the code so that only selected users can edit the data in the hidden columns.

Please show me a code that would hide all columns except A:G that requires a password to unhide in.

Thank you in advance.
 
Upvote 0
Once you protect a sheet with a password, you cannot unhide columns without unprotecting the sheet first.
 
Upvote 0
Hi marka87uk,

Thanks for your reply. I knew that would be the case. What I was looking for, if possible, is a vba code that would hide the columns I selected even without protecting the sheets.

My file has a macro on it and protecting the sheet will prevent the macro from running, that is why the workaround.
 
Upvote 0
Once you protect a sheet with a password, you cannot unhide columns without unprotecting the sheet first.

This is true...

Fortunately there is code that can unprotect a sheet with a password.

Rich (BB code):
Sheets("SheetName").Unprotect Password:="passwordgoeshere"
 
'do some stuff..like
Range("H:IV").EntireColumn.Hidden = True
 
Sheets("SheetName").Protect Password:="passwordgoeshere"
 
Upvote 0
Hello All,

This thread was extremely helpful to me and it worked!!:)

I did revise it for my needs though to:
Sub HideColumnsONECHOICE()
Dim MyRange As Range
Set MyRange = Range("A:A")
MyRange.EntireColumn.Hidden = Not MyRange.EntireColumn.Hidden
End Sub


If you could please help me with one change.
The code above will hide column "A" on the ative sheet - which is great.

But.....I need it to DELETE column "A" on any sheets within my workbook EXCEPT the one called "Master".

I can show you a part of the code that I think I should use but I can tell that it's not correct to work on all sheets (except the master)

End With
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft



Thank you so much for your help,

Juicy,
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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