I am trying to do VBA code to conditionally lock specific cells based on the content of another cell.
Not having much luck...any help appreciated!
This is what I tried - but I can't get it to work:
Not having much luck...any help appreciated!
This is what I tried - but I can't get it to work:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
Dim r As Variant
If Not Intersect(Range("D9:M9"), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Me.Unprotect Password:="mypassword"
For Each cel In Intersect(Range("D9:M9"), Target)
' Unlock all cells
For Each r In Array(16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 70, 72, 74, 76)
With Cells(r, cel.Column)
.Locked = False
.ClearContents
End With
Next r
Select Case cel.Value
Case "YES"
For Each r In Array(18, 26, 30, 34, 38, 47, 49, 53, 74, 76, 78)
Cells(r, cel.Column).Locked = True
Next r
Case "NO"
For Each r In Array(61)
Cells(r, cel.Column).Locked = True
Next r
End Select
Next cel
Me.Protect Password:="mypassword"
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
Last edited by a moderator: