仅当允许过滤 = True 时,才取消保护工作表提示



我有两个非常简单的子来保护和取消保护带有密码的工作表。Subs 工作得很好,直到我添加"允许过滤:=True"参数。

添加该参数后,系统会在取消保护工作表时提示我输入密码。但是,如果我按"取消",工作表将不受保护(因为它应该受到保护)。

因此,密码提示似乎完全是多余的,但是有人可以帮助我摆脱它吗?

这两个潜艇是:

Sub LockSheet()
If ActiveSheet.Protect = False Then
ActiveSheet.Protect Password:="TopSecretPW", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End If
End Sub

Sub UnlockSheet()
If ActiveSheet.Unprotect = False Then
ActiveSheet.Unprotect Password:="TopSecretPW"
End If
End Sub

如果我使用此宏 - 没有 AllowFiltering:=True 参数 - 系统不会提示我输入 pw。

Sub LockSheet()
If ActiveSheet.Protect = False Then
ActiveSheet.Protect Password:="TopSecretPW", DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
End Sub

奖励信息:过滤器和保护在参数集下工作得很好,如果不输入正确的 PW,我就无法从菜单中取消保护工作表。我想埋葬的只是那个不请自来的PW提示。

问题在于您如何检查工作表是否受到保护。 ActiveSheet.Protect 是一个方法,而不是工作表对象的属性。 而不是使用If ActiveSheet.Protect使用If ActiveSheet.ProtectContents. ProtectContents 是工作表对象的属性,如果工作表受保护,则返回 true;如果工作表不受保护,则返回 false。 修改后的代码应如下所示:

Sub LockSheet()
If ActiveSheet.ProtectContents = False Then
    ActiveSheet.Protect Password:="TopSecretPW", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True
End If
End Sub

Sub UnlockSheet()
If ActiveSheet.ProtectContents = True Then
    ActiveSheet.Unprotect Password:="TopSecretPW"
End If
End Sub

最新更新