【问题标题】:Protecting Macro Enabled worksheet in a workbook保护工作簿中启用宏的工作表
【发布时间】:2016-10-14 17:28:00
【问题描述】:

随着时间的推移,我有一个 4 页的员工工作簿。前 3 张表是其他人添加数据的地方。第 4 页是数据相加的地方。在第四张纸上,我启用了一个自动排序宏来不断地重新排序我在几个不同表上的数据。我想保护这第四张纸以防篡改,但我发现每当我保护这张纸时,公式仍然有效,但宏不起作用。我知道宏有效,因为每当我取消保护工作表时,宏都会再次激活。我是否需要在我的宏中添加一些东西以使其在保护模式下工作,或者我只是做错了什么,或者这是 excel 2010 无法做到的?这是我当前使用的宏:

Private Sub Worksheet_Activate()
On Error Resume Next
    Range("c1").Sort Key1:=Range("c2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
On Error Resume Next
    Range("k1").Sort Key1:=Range("k2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom

On Error Resume Next
    Range("o1").Sort Key1:=Range("o2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
On Error Resume Next
    Range("s1").Sort Key1:=Range("s2"), _
      Order1:=xlAscending, Header:=xlYes, _
      OrderCustom:=1, MatchCase:=False, _
      Orientation:=xlTopToBottom
End Sub

【问题讨论】:

  • 当您保护工作表时,请尝试在“保护工作表”对话框中出现的选项列表中启用“排序”(可能需要向下滚动才能看到它)。
  • 您可以在排序宏中取消保护工作表,然后在最后重新保护。顺便说一句,On Error Resume Next 在您取消之前一直有效 - 您只需要其中一行...

标签: vba excel macros


【解决方案1】:

你有一个可以一起工作的朋友。这位朋友叫userinterfaceonly

在代码的开头,添加以下内容:

    ActiveSheet.Unprotect Password:="Whatever"
    ActiveSheet.Protect _
        Password:= "Whatever", _
        userinterfaceonly:=True

这将删除工作表保护,然后使用 UserInterfaceOnly 属性重新激活它,该属性允许由宏进行更改,但不能由用户进行。不幸的是,该属性无法保存,因此我们需要在文件打开或工作表激活时运行代码。

【讨论】:

    【解决方案2】:

    是的,我认为您需要在调用排序功能之前解锁工作表

    Private Sub Worksheet_Activate()
        Set sheet = ActiveSheet
        sheet.Unprotect Password:="password"
        sheet.Range({whatever range you want}).Locked = False    
        On Error Resume Next
            Range("c1").Sort Key1:=Range("c2"), _
            Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom
        On Error Resume Next
          Range("k1").Sort Key1:=Range("k2"), _
          Order1:=xlAscending, Header:=xlYes, _
          OrderCustom:=1, MatchCase:=False, _
          Orientation:=xlTopToBottom
    sheet.protect Password:="password"
    sheet.Range({whatever range you want}).Locked = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多