假定A1单元为1,B1锁定,A1变为2时解锁。以下代码能满足你的要求:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
ActiveSheet.Unprotect
If Target = 1 Then Range("B1").Locked = True
If Target = 2 Then Range("B1").Locked = False
ActiveSheet.Protect
End Sub
注意两点:一是代码不能写在模块里,就写在工作表代码区,二是在写代码前先将所有单元格解锁:
这个要代码的事件才能完成。工作表保护,可设密码,不过容易破解。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
If Target = 1 Then
ActiveSheet.Unprotect
[a2].Locked = True
ActiveSheet.Protect
Else
ActiveSheet.Unprotect
[a2].Locked = False
ActiveSheet.Protect
End If
End Sub
如果a1=1则锁定a2