简体   繁体   中英

Need for review the VBA code

I try to lock a cell base on another cell value. I used the codes some of you provided and worked perfect. I have some other codes with active sheet change too and VBA issue an error. With the following code is working, but when the condition changed, I expect to the cell back to normal and be unlocked, but still locked.

The code is:

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

Thank you for your time.

Is Your "N/A" given by a formula? because if it is you have to use

isError([F14])

And just a word instead of

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

you can Use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM