|
Sub ProtectRowsBasedOnDate()
Dim ws As Worksheet
Dim lastRow As Long
Dim currentDate As Date
Dim cell As Range
Dim protectRange As Range
Dim editableRange As Range
Dim editRange As AllowEditRange
Set ws = ActiveSheet
currentDate = Date
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For Each cell In ws.Range("A1:A" & lastRow)
If IsDate(cell.Value) And cell.Value <> currentDate Then
If protectRange Is Nothing Then
Set protectRange = cell.EntireRow
Else
Set protectRange = Application.Union(protectRange, cell.EntireRow)
End If
Else
If editableRange Is Nothing Then
Set editableRange = cell.EntireRow
Else
Set editableRange = Application.Union(editableRange, cell.EntireRow)
End If
End If
Next cell
If Not editableRange Is Nothing Then
Set editRange = ws.Protection.AllowEditRanges.Add("EditableRange", editableRange)
End If
If Not protectRange Is Nothing Then
ws.Protect Password:="yourpassword", UserInterfaceOnly:=True
End If
End Sub
|
|