locking表不能按预期工作

我想根据另一张表单元格的值locking/解锁给定的表格。 我为表单form工作表更改了以下代码:

 Application.ScreenUpdating = False Status = Sheets("form").Range("J2") If Status = "Active" Then Sheets("overview").Unprotect "password" 'MsgBox "The template is now unlocked" Else Sheets("overview").Protect "password" 'MsgBox "The template is locked" End If Application.ScreenUpdating = True 

但是,当Status不是active我仍然可以在overview更改某些单元格,以获取“无法设置Range类的隐藏属性”popup窗口。 错误在哪里?

Worksheet_Change事件(在您的form工作表代码)上尝试以下代码:

 Private Sub Worksheet_Change(ByVal Target As Range) Application.ScreenUpdating = False Status = Me.Range("J2") If Status = "Active" Then Sheets("overview").Unprotect "password" 'MsgBox "The template is now unlocked" Else Sheets("overview").Protect "password" 'MsgBox "The template is locked" End If Application.ScreenUpdating = True End Sub