允许用户在 sheet 被锁定时隐藏列

Allowing user to hide columns when sheet is locked

我正在使用以下 VBA 脚本在工作簿中的 sheet 被锁定时为用户提供某些权限。我无法弄清楚如何添加一行也允许用户隐藏和取消隐藏列。有什么建议吗?

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

将研究一种方法来检查最后的用户操作是否实际上是隐藏/取消隐藏,而不是其他。但现在应该允许用户隐藏/取消隐藏。

对于列:

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

对于行:

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

此脚本应该有助于限制用户 activity 只能调整列宽。 (lastAction把隐藏列描述为调整宽度,一定是隐藏列真的只是一个最小化列宽的函数,而不是什么特殊的动作)

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

为此,您只需更改锁定 sheet 时允许用户处理的部分。我 select 编辑了除“select 锁定的单元格”之外的所有内容,然后它就可以工作了。这意味着代码仍然隐藏在我不想编辑的单元格中,但基于我的 VBA 代码的隐藏展开按钮正在工作。