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