VBA to Protect Worksheet & allow formatting columns but keep certain columns hidden

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I am trying to figure out a way to protect my worksheet with VBA but include the following criteria:
  • Not allow users to unhide columns or rows
  • Allow users to format columns and rows (ie readjust column width, etc...)

When I enable my sheet protection with "Allow Column Formatting" checked, I can adjust columns widths but can also unhide columns that I want to remain hidden. Any thoughts on how to get around this?

Thanks for your help!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
The following code will disable users from hiding columns and rows. If you have hidden columns or rows that you don't want to be unhidden, then this code can be tweaked to accommodate that, would just need to know which rows and/or columns.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For Each Column In ActiveSheet.Columns
        If Column.ColumnWidth = 0 Then
            MsgBox "Hiding columns has been disabled for this workbook"
            Application.Undo
        End If
    Next
    For Each Row In ActiveSheet.UsedRange.Rows
        If Row.RowHeight = 0 Then
            MsgBox "Hiding rows has been disabled for this workbook"
            Application.Undo
        End If
    Next
End Sub
And you would paste this into the worksheet code for the sheet in question.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,358
Messages
6,136,093
Members
449,991
Latest member
IslandofBDA

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