Hide/Unhide columns when protecting/unprotecting sheets

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a protected sheet where column AA and AB are for comments. I would like to be able to type into this column and when the sheet is protected the columns are hidden.
They should remain hidden until the sheet is unprotected.

I dont want a button or toggle switch or anything like that. I would like the columns to be hidden or unhidden based on if the sheet is protected or unprotected.

thanks

Rory
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Have a try with what I came up with. Your columns will Hide/UnHide only when you select a different cell since I'm using event Worksheet_SelectionChange to trigger the check of the sheet's state Protected/UnProtected but I think this won't be a problem. The protection can be managed by another macro or manually. Paste the macro in the sheet's module and see if you can integrate it into your project.
VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If ActiveSheet.ProtectContents = True Then
        ActiveSheet.UnProtect
        Columns("AA:AB").EntireColumn.Hidden = True
        ActiveSheet.Protect
    Else
        Columns("AA:AB").EntireColumn.Hidden = False
    End If
End Sub
 
Upvote 0
Solution
Have a try with what I came up with. Your columns will Hide/UnHide only when you select a different cell since I'm using event Worksheet_SelectionChange to trigger the check of the sheet's state Protected/UnProtected but I think this won't be a problem. The protection can be managed by another macro or manually. Paste the macro in the sheet's module and see if you can integrate it into your project.
VBA Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If ActiveSheet.ProtectContents = True Then
        ActiveSheet.UnProtect
        Columns("AA:AB").EntireColumn.Hidden = True
        ActiveSheet.Protect
    Else
        Columns("AA:AB").EntireColumn.Hidden = False
    End If
End Sub
Brilliant. Works exactly as I need :)
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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