Prevent Data entry once date has passed

I've come up with this code to prevent employees from retroactively editing data

The date cells are located at A3:A45

Yet I get a runtime 1004 error messege when trying to push the neet play button as I like to call it,

Ive marked the problematic spot with a strikethrough on here..but I sure do not know what is wrong with it.

 

I would really appreciate your assistance

Thank you!

Eyal

 

 

Const SHEET_PASSWORD As String = "1234"
 
 
 
Sub ProtectRows()
 
    'ActiveSheet.Protect Contents:=True

    Dim p As Protection
 
    Set p = ActiveSheet.Protection
 
    Dim AllwedRange As AllowEditRange
 
    For Each AllwedRange In p.AllowEditRanges
 
        AllwedRange.Delete
 
    Next AllwedRange
 
    Dim r As Range
 
    'Set r = ActiveSheet.UsedRange.Columns("A")

    Set r = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
 
    Dim rCell As Range
 
    Dim i As Integer
 
    For Each rCell In r.Cells
 
        If rCell.Value >= DateValue(Now()) Then
 
            Set AllwedRange = p.AllowEditRanges.Add("Editable" & i, Rows(rCell.Row))
 
            i = i + 1
 
        End If
 
    Next rCell
 
    ActiveSheet.Protect Password:=SHEET_PASSWORD, AllowSorting:=True, AllowFiltering:=True
 
End Sub
Nick's picture

Lock data after a certain date

so basically, you're looking for a way to Lock data after a certain date.

Why not unlock all empty cells, then lock the populated cells using the worksheet_change event if today's date > your target date..

Nick