You can use the Workbook_Open event, in the ThisWorkbook module:
public sub Workbook_Open()
Call RefreshPageLock(worksheets("Sheet1"))
end sub
In a new module:
public sub RefreshPageLock(byval wksCurr as worksheet)
Dim NewRange As String, _
TopRow As Long
With wkscurr
If .Range("V1").Value <> "" And .Range("V2").Value <> "" Then
TopRow = .Range("V3").Value / 3
NewRange = .Range(.Cells(22 - TopRow, "B"), .Cells(22 - TopRow, "U")).Address(0, 0) & ":B22"
.Unprotect "password"
.Cells.Locked = True
.Range(NewRange).Locked = False
.Protect "password"
.EnableSelection = xlUnlockedCells
.Protect
EditMode = .Name
End If
End With
exit_routine:
Application.EnableEvents = True
Application.ScreenUpdating = True
end sub
I would recommend changing your original code from using the worksheet_selectionchange event to using the worksheet_change event:
private sub Worksheet_Change(byval Target as Excel.Range)
if not intersect(Target, Range("V1:V3")) is nothing then
call refreshpagelock(me)
endif
end sub
The selectionchange event will trigger everytime a new cell is selected, which typically happens more often than changing a cell.
I'm not sure that I have the worksheet references right, you are referring to both Sheet1 and Sheet2. That can be easily fixed if this is close to what you want.
Bookmarks