【问题标题】:Worksheet protection set using ws.protect - but doesnt unprotect using the Menu (Review - Unprotect sheet)使用 ws.protect 设置工作表保护 - 但不使用菜单取消保护(查看 - 取消保护工作表)
【发布时间】:2020-10-09 08:13:15
【问题描述】:

我有以下代码来保护 Excel 2007 中工作簿的工作表

Private Sub password_protectallsheets()
For Each ws In Worksheets
ws.protect Password = "edc1"
Next
End Sub

但是当我尝试通过 Excel 2007 菜单(查看 -> 取消保护工作表)使用密码取消保护工作表时,它显示您提供的密码不正确。

非常感谢任何帮助。

【问题讨论】:

    标签: excel vba protection


    【解决方案1】:

    密码参数需要用“:=”指定 - 而不仅仅是“=”。这对于一般方法的参数是正确的:

    ws.protect Password := "edc1"
    

    要让编译器捕获这些类型的错误,请始终在模块的开头使用 Option Explicit,就像 JMax 上面所做的那样。当您这样做时,当您忘记“:”选项时,编译器会给您一个“变量未定义”错误,显式选项将为您节省大量时间来处理其他类型的变量声明错误。要为所有新模块启用它,请检查 Tools>Options>Edit>Require Variable Declaration in the VBE。

    我无法弄清楚的部分是为什么在没有“:”的代码中完全分配了密码。我本来希望工作表受到保护,但没有密码。

    【讨论】:

      【解决方案2】:

      试试:

      Option Explicit
      
      Private Sub password_protectallsheets()
          Dim ws As Worksheet
          For Each ws In Worksheets
              ws.Protect "edc1"
          Next
      End Sub
      

      没有password 关键参数。
      它适用于我的 Excel 2007。

      顺便说一句,一定要复制/粘贴密码,或者一定要检查最后一个字符是1(一)还是l(小写字母L)。

      【讨论】:

        【解决方案3】:

        Ws.Unprotect 真的保护工作表吗? - surly 它取消保护它没有设置保护的工作表

        在宏中设置保护为什么不记录保护工作表并查看代码

        【讨论】:

          【解决方案4】:

          这个ws.protect Password = "edc1"ws.protect Password := "edc1" 没有工作! 需要通过变量发送密码,修复这个bug!

          试试这个,它对我有用:

          一张纸

          Sub Protect()
          Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
          Dim strPassword As String: strPassword = "YourPassword"
          ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
                  UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
                  AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
                  AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
                  AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
          End Sub
          

          Sub UnProtect()
          Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
          Dim strPassword As String: strPassword = "YourPassword"
          ws.Unprotect Password:=strPassword
          End Sub
          

          所有工作表

          Sub ProtectAllSheets()
              Dim ws As Worksheet
              Dim strPassword As String: strPassword = "YourPassword"
              
              For Each ws In Worksheets
              ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
                  UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
                  AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
                  AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
                  AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
              Next ws
              
          End Sub
          

          Sub UnProtectAllSheets()
              Dim ws As Worksheet
              Dim strPassword As String: strPassword = "YourPassword"
              
              For Each ws In Worksheets
                  ws.Unprotect Password:=strPassword
              Next ws
              
          End Sub
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-07-22
            • 2019-03-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多