Search code examples
excelvbashow-hide

Allowing user to hide columns when sheet is locked


I am using the following VBA script to provide users certain permissions when a sheet in a workbook is locked. I cannot figure out how to add a line that would also allow the user to hide and unhide columns. Any suggestions?

Sub EnableOutlining()
'Update 20140603
Dim xWs As Worksheet
Set xWs = Application.ActiveSheet
Dim xPws As String
xPws = Application.InputBox("Password:", xTitleId, "", Type:=2)
xWs.Protect Password:=xPws, Userinterfaceonly:=True
xWs.EnableOutlining = True
xWs.EnableOutlining = True
xWs.EnableAutoFilter = True
xWs.EnableFormatConditionsCalculation = True
End Sub

Solution

  • Going to work on a way to check that last user action was in fact hide / unhide and nothing else. But for now should allow user to hide / unhide.

    For columns:

    xWs.protect Password:= "1234",AllowFormattingColumns:= true
    

    For rows:

    xWs.protect Password:= "1234",AllowFormattingRows:= true
    

    This script should help limit user activity to only adjusting column widths. (lastAction describes hiding columns as adjust width, must be that hiding columns is really just a function that minimizes column width, not some special action)

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim lastAction As String
            lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
    
        If lastAction <> "Column Width" Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "PLEASE ONLY HIDE OR UNHIDE COLUMNS"
            Application.EnableEvents = True
        End If
    End Sub