取消保护选定范围时出现运行时错误

取消保护选定范围时出现运行时错误

当我通过 (UnProtectRange)-VBA 取消保护选定区域并且选定区域未取消保护时,出现运行时错误

我已经使用(ProtectRange)-VBA 宏通过输入框保护范围,并且所选范围已通过另一个输入框通过给定的密码保护

当我尝试通过 (UnProtectRange)-VBA 宏取消保护范围时,出现了运行时错误

Sub ProtectRange()
  Dim rng As Range
  Dim pws As String
  Dim ps As Variant
  pws = "123"
  Set rng = Application.InputBox("Select The Range to Protect:", "Protect", Type:=8)
  ps = InputBox("Enter Password to Protect")
  Cells.Locked = False
  rng.Locked = True
  ActiveSheet.Protect Password:=pws
  MsgBox ("Range " & "(" & rng.Address & ")" & " Locked")
End Sub



Sub UnProtectRange()
  Dim rng As Range
  Dim pws As String
  Dim ps As Variant
  pws = "123"
  Set rng = Application.InputBox("Select The Range to UnProtect:", "UnProtect", Type:=8)
  ps = InputBox("Enter Password to UnProtect")
  Cells.Locked = False
  rng.Locked = True
  ActiveSheet.UnProtect Password:=pws
  MsgBox ("Range " & "(" & rng.Address & ")" & " Un-Locked")
End Sub

答案1

我用这种方法解决了:


Sub UnProtectRange()
  Dim rng As Range
  Dim pws As String
  Dim ps As Variant
  pws = "123"
  Set rng = Application.InputBox("Select The Range to UnProtect:", "UnProtect", Type:=8)
  ps = InputBox("Enter Password to UnProtect")
  ActiveSheet.UnProtect Password:=pws
  Cells.Locked = False
  rng.Locked = True
  MsgBox ("Range " & "(" & rng.Address & ")" & " Un-Locked")
End Sub


相关内容