Option Explicit
Public Const sHDName = "ProtectSupport", sLDName = "LockDate", sWBLName = "WBLocked"
Public Const sPW = "MyPW" '<<<<< Change to suit your password
Dim bReset As Boolean
Sub FirstTimeSetup()
'Macro call by Workbook_Open to set and store the lock date
'This is stored in a sheet which will be hidden
Dim wsWS As Worksheet
Application.ScreenUpdating = False
On Error Resume Next
Set wsWS = Sheets(sHDName)
On Error GoTo 0
If wsWS Is Nothing Then 'sheet does not exist, create
With ThisWorkbook
Set wsWS = .Sheets.Add(after:=.Sheets(.Sheets.Count))
wsWS.Name = sHDName
.Names.Add Name:=sLDName, RefersTo:=wsWS.Range("A2")
.Names.Add Name:=sWBLName, RefersTo:=wsWS.Range("A5")
End With
With Range(sLDName)
.Offset(-1, 0) = sLDName
.Value = GetDate
.Offset(2, 0) = sWBLName
End With
wsWS.Visible = xlSheetVeryHidden 'Sheet is not visible in the sheet list, only in VBA
End If
If bReset Then
With Range(sLDName)
.Value = GetDate
End With
End If
Application.ScreenUpdating = True
End Sub
Function GetDate() As Date
'Get the lock date
Dim vD As Variant
Do
vD = InputBox(prompt:="Please enter date after which the sheets in" & vbCrLf & _
"this workbook need to be locked." & vbCrLf & _
"Enter as " & Format(Date, "Short Date") & ".", _
Title:="Workbook lock date required")
Loop While Not IsDate(vD)
GetDate = CDate(vD)
End Function
Sub LockSheets(sPWd As String)
'Called by Workbook_Open. Locks all sheets
Dim wsWS As Worksheet
'first unlock all sheets as we will be changing the lock status of cells
UnlockSheets sPWd
'then on each sheet (not on our support sheet) lock all cells and protect
For Each wsWS In ThisWorkbook.Worksheets
If wsWS.Name <> sHDName Then
wsWS.Cells.Locked = True
wsWS.Protect sPWd, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
AllowSorting:=True, AllowFiltering:=True
wsWS.EnableSelection = xlUnlockedCells
End If
Next wsWS
End Sub
Sub UnlockSheets(sPWd As String)
'Unlocks all sheets. Sets all cells to editable!
Dim wsWS As Worksheet
For Each wsWS In ThisWorkbook.Worksheets
wsWS.Unprotect sPWd
Next wsWS
End Sub
Sub SetWB2Locked(sPWd As String)
Range(sWBLName) = True 'to mark that this process has been carried out
LockSheets sPWd
MsgBox prompt:="The sheets in this workbook have just been locked as the expiry date has passed.", _
Title:="Sheets locked"
End Sub
Sub ResetLockDate()
'Allows authorised user to change the lock date
Dim vP
Dim dDt As Date
Dim sMsg As String
vP = InputBox(prompt:="Please enter password to change the lockdate for this workbook", _
Title:="Password required")
If vP = sPW Then
dDt = GetDate
If dDt <= Date And Date > Range(sLDName) Then
MsgBox "Date provided does not make sense: the workbook is already locked. No action taken."
Else
Range(sLDName) = dDt
If Range(sWBLName) Then
UnlockSheets sPW
Range(sWBLName) = False
sMsg = "All worksheets have been unlocked. " & vbCrLf
End If
MsgBox sMsg & "Lock date set to: " & Format(dDt, "short date")
End If
Else
MsgBox "Incorrect or no password given. No action taken"
End If
End Sub