需要查看VBA代码

时间:2017-08-28 02:13:38

标签: vba excel-vba locking cell excel

我尝试将一个单元格锁定在另一个单元格值上。我使用了你提供的代码并且工作得很完美。我有一些其他代码也有活动表更改,VBA发出错误。 以下代码正常工作,但当条件发生变化时,我希望单元格恢复正常并解锁,但仍然锁定。

代码是:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$7" Then
ActiveSheet.Unprotect ("mn")
If Target.Value = 10.4 Then Rows("21:26").EntireRow.Hidden = True
If Target.Value = 10.4 Then Rows("16").EntireRow.Hidden = True
If Target.Value <> 10.4 Then Rows("21:26").EntireRow.Hidden = False
If Target.Value <> 10.4 Then Rows("16").EntireRow.Hidden = False
End If
ActiveSheet.Protect ("mn")
End Sub

Private Sub Worksheet_Activate()
ActiveSheet.Unprotect ("mn")
If [E14] = "N/A" Then
[G14].Locked = True
'Remove locked property if E14's value is anything else or is deleted.
Else
[G14].Locked = False
'Optional, reprotect sheet
End If

If [E28] = "N/A" Then
[g28].Locked = True
'Remove locked property if E28's value is anything else or is deleted.
Else
[g28].Locked = False
'Optional, reprotect sheet
End If

If [E38] = "N/A" Then
[g38].Locked = True
'Remove locked property if E38's value is anything else or is deleted.
Else
[g38].Locked = False
'Optional, reprotect sheet
End If
ActiveSheet.Protect ("mn")
End Sub

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您的“N / A”是否由公式给出?因为如果是你必须使用

isError([F14])

只是一个词而不是

If [E14] = "N/A" Then
    [G14].Locked = True
    'Remove locked property if E14's value is anything else or is deleted.
Else
    [G14].Locked = False
    'Optional, reprotect sheet
End If

你可以使用

[G14].Locked = ([E14] = "N/A")