代码之家  ›  专栏  ›  技术社区  ›  Sjoerd

在Excel中捕获工作表取消保护事件

  •  2
  • Sjoerd  · 技术社区  · 17 年前

    我有一个受保护的Excel工作表,没有密码。
    有人有主意吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Fionnuala    17 年前

    Sub UnprotectTrap()
    If ActiveSheet.ProtectContents = True Then
        MsgBox "Tut,tut!"
        ActiveSheet.Unprotect
    Else
        ActiveSheet.Protect
    
    End If
    End Sub
    
        2
  •  0
  •   dbb    17 年前

    无法阻止用户解除对工作表的保护,但如果用户保存工作簿而不重新保护工作表,您可以向他们发出警告。

    在工作簿模块中,输入以下代码或类似代码

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If Sheets("MyProtectedSheet").ProtectContents = False Then
        MsgBox "The sheet 'MyProtectedSheet' should not be left unprotected. I will protect it before saving", vbInformation
        Sheets("MyProtectedSheet").Protect
      End If
    End Sub