Hi,
is there anyway to improve the code below, i would need to duplicate this 365 times to make it work, im sure there would be a much simpler way to do this.
For Settings page check if yes/no to lock so Jan cells are O2 to O32 then Feb is Q2 to Q30 (inc 29th day) and so on
then on the Jan sheet, the Range is E6:E405 and is every 5 columns, so E,J,O,T,Y etc
is there anyway to improve the code below, i would need to duplicate this 365 times to make it work, im sure there would be a much simpler way to do this.
For Settings page check if yes/no to lock so Jan cells are O2 to O32 then Feb is Q2 to Q30 (inc 29th day) and so on
then on the Jan sheet, the Range is E6:E405 and is every 5 columns, so E,J,O,T,Y etc
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Sheets("Jan").Unprotect
'Sheets("Feb").Unprotect
'Sheets("Mar").Unprotect
'Sheets("Apr").Unprotect
'Sheets("May").Unprotect
'Sheets("Jun").Unprotect
'Sheets("Jul").Unprotect
'Sheets("Aug").Unprotect
'Sheets("Sep").Unprotect
'Sheets("Oct").Unprotect
'Sheets("Nov").Unprotect
'Sheets("Dec").Unprotect
Dim rCell As Range
If Range("O2") = "No" Then
For Each rCell In Worksheets("Jan").Range("E6:E405")
rCell.Offset(0, 0).Locked = False
rCell.Offset(0, 1).Locked = False
rCell.Offset(0, -1).Locked = False
rCell.Offset(0, -2).Locked = False
rCell.Offset(0, -3).Locked = False
Next rCell
ElseIf Range("O2") = "Yes" Then
For Each rCell In Worksheets("Jan").Range("E6:E405")
If rCell.Offset(0, 0).Value = "Holiday" Or rCell.Offset(0, 0).Value = "Lieu" Or rCell.Offset(0, 0).Value = "Unpaid" Or rCell.Offset(0, 0).Value = "Float Day" Then
rCell.Offset(0, 0).Locked = True
rCell.Offset(0, 1).Locked = True
rCell.Offset(0, -1).Locked = True
rCell.Offset(0, -2).Locked = True
rCell.Offset(0, -3).Locked = True
End If
Next rCell
End If
If Range("O3") = "No" Then
For Each rCell In Worksheets("Jan").Range("J6:J405")
rCell.Offset(0, 0).Locked = False
rCell.Offset(0, 1).Locked = False
rCell.Offset(0, -1).Locked = False
rCell.Offset(0, -2).Locked = False
rCell.Offset(0, -3).Locked = False
Next rCell
ElseIf Range("O3") = "Yes" Then
For Each rCell In Worksheets("Jan").Range("J6:J405")
If rCell.Offset(0, 0).Value = "Holiday" Or rCell.Offset(0, 0).Value = "Lieu" Or rCell.Offset(0, 0).Value = "Unpaid" Or rCell.Offset(0, 0).Value = "Float Day" Then
rCell.Offset(0, 0).Locked = True
rCell.Offset(0, 1).Locked = True
rCell.Offset(0, -1).Locked = True
rCell.Offset(0, -2).Locked = True
rCell.Offset(0, -3).Locked = True
End If
Next rCell
End If
If Range("O4") = "No" Then
For Each rCell In Worksheets("Jan").Range("O6:O405")
rCell.Offset(0, 0).Locked = False
rCell.Offset(0, 1).Locked = False
rCell.Offset(0, -1).Locked = False
rCell.Offset(0, -2).Locked = False
rCell.Offset(0, -3).Locked = False
Next rCell
ElseIf Range("O4") = "Yes" Then
For Each rCell In Worksheets("Jan").Range("O6:O405")
If rCell.Offset(0, 0).Value = "Holiday" Or rCell.Offset(0, 0).Value = "Lieu" Or rCell.Offset(0, 0).Value = "Unpaid" Or rCell.Offset(0, 0).Value = "Float Day" Then
rCell.Offset(0, 0).Locked = True
rCell.Offset(0, 1).Locked = True
rCell.Offset(0, -1).Locked = True
rCell.Offset(0, -2).Locked = True
rCell.Offset(0, -3).Locked = True
End If
Next rCell
End If
If Range("O5") = "No" Then
For Each rCell In Worksheets("Jan").Range("T6:T405")
rCell.Offset(0, 0).Locked = False
rCell.Offset(0, 1).Locked = False
rCell.Offset(0, -1).Locked = False
rCell.Offset(0, -2).Locked = False
rCell.Offset(0, -3).Locked = False
Next rCell
ElseIf Range("O5") = "Yes" Then
For Each rCell In Worksheets("Jan").Range("T6:T405")
If rCell.Offset(0, 0).Value = "Holiday" Or rCell.Offset(0, 0).Value = "Lieu" Or rCell.Offset(0, 0).Value = "Unpaid" Or rCell.Offset(0, 0).Value = "Float Day" Then
rCell.Offset(0, 0).Locked = True
rCell.Offset(0, 1).Locked = True
rCell.Offset(0, -1).Locked = True
rCell.Offset(0, -2).Locked = True
rCell.Offset(0, -3).Locked = True
End If
Next rCell
End If
'Sheets("Jan").Protect
'Sheets("Feb").Protect
'Sheets("Mar").Protect
'Sheets("Apr").Protect
'Sheets("May").Protect
'Sheets("Jun").Protect
'Sheets("Jul").Protect
'Sheets("Aug").Protect
'Sheets("Sep").Protect
'Sheets("Oct").Protect
'Sheets("Nov").Protect
'Sheets("Dec").Protect
End Sub