Dim varOldValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1:I20")) Is Nothing Then
If Cells(Target.Row, 1).Value = Application.Evaluate("=TODAY()-2") Then
Target.Formula = varOldValue
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
varOldValue = Target.Formula
End Sub
Sub LockCells()
Application.ScreenUpdating = False
For i = 1 To 100
If Range("A" & i) + 3 > Date Then Range("A" & i & ":I" & i).Locked = True
Next i
Application.ScreenUpdating = True
End Sub
Put this in your module and run it. It will do the job for cells A1:A100 and go out to column I on the cells that need to be locked.
PHP:Sub LockCells() Application.ScreenUpdating = False For i = 1 To 100 If Range("A" & i) + 3 > Date Then Range("A" & i & ":I" & i).Locked = True Next i Application.ScreenUpdating = True End Sub
Sub LockCells()
Application.ScreenUpdating = False
On Error Resume Next
For i = 1 To 100
If Range("A" & i) + 3 > Date Then Range("A" & i & ":I" & i).Locked = True
Next i
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Open()
Dim cl As Range
Sheet1.Unprotect "password"
For Each cl In Sheet1.Range("$A$2:$A" & Range("$A$65536").End(xlUp).Row)
If cl < Date - 2 Then
Range(Cells(cl.Row, 1), Cells(cl.Row, 8)).Locked = True
End If
Next cl
Sheet1.Protect "password"
End Sub
A mismatch error will occur if you have something other than a date in the cells the macro is testing for the date condition. Its an easy fix, just use the code below and it will skip over the errors.
HTML:Sub LockCells() Application.ScreenUpdating = False On Error Resume Next For i = 1 To 100 If Range("A" & i) + 3 > Date Then Range("A" & i & ":I" & i).Locked = True Next i On Error GoTo 0 Application.ScreenUpdating = True End Sub