I can't see it working for a selection of cells as the code will have to check each cell the range and if any cell in that range is changed the code will start all over again - effectively creating a loop.
I've therefore put a message in the following to inform the user that the code only works on an individual cell.
HTH
Robert
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strMyPassword, strColumnChg As String
Dim lngLastRow, lngRowChg As Long
'Set your desired password to this variable.
strMyPassword = "YourPasswordHere"
ActiveSheet.Unprotect Password:=strMyPassword
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
strColumnChg = Left(Target.Address(False, False), 1)
lngRowChg = Val(Mid(Target.Address(False, False), 2, Len(Target.Address(False, False)) - 1))
'Code will only work if: _
1. The change has occurred in Column E _
2. The change is from row 17 to the last row used in Column A (inclusive), and _
3. A range (which Column E forms a part) has not been selected.
If strColumnChg = "E" And _
lngRowChg >= 17 And _
lngRowChg <= lngLastRow Then
If InStr(Target.Address(False, False), ":") > 0 Then
MsgBox "The Worksheet_Change event is not coded to work on a range.", _
vbExclamation, "Case Type Change Editor"
Exit Sub
End If
If Target.Value = "DI" Or _
Target.Value = "LTC" Or _
Target.Value = "Z" Then
With Range("F" & lngRowChg)
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.Locked = False
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.ClearContents
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.Interior.Color = RGB(192, 192, 192)
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.Locked = True
End With
Else
With Range("F" & lngRowChg)
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.Locked = False
If ActiveSheet.ProtectContents = True Then
ActiveSheet.Unprotect Password:=strMyPassword
End If
.Interior.ColorIndex = xlNone
End With
End If
Else
Exit Sub
End If
ActiveSheet.Protect Password:=strMyPassword
End Sub
HTH
Robert
Bookmarks