Hi,
Can anyone shed some light on why this code isn't working. This is code from another site and some that had been sent to me.
What I'm after is some code that locks the worksheets depending on time off day. I currently have a day/night shift and I need only their cells open when they are on shift. The code seems to loop with the timers that are set.
Can anyone shed some light on why this code isn't working. This is code from another site and some that had been sent to me.
What I'm after is some code that locks the worksheets depending on time off day. I currently have a day/night shift and I need only their cells open when they are on shift. The code seems to loop with the timers that are set.
VBA Code:
Private Sub Workbook_Open()
Dim CurrTime As Date
Dim LockTime As Date
Dim UnLockTime As Date
CurrTime = Time ' Get the Current Time
LockTime = TimeValue("06:05:00") ' Specify the Lock Time
UnLockTime = TimeValue("18:00:00") ' Specify the Unlock Time
If CurrTime > UnLockTime Then ' If the sheet is opened later than the specified unlock time then we want make sure the cells are unlocked.
Application.Run "LockSheet"
Else
If CurrTime < LockTime Then ' Check to see if the sheet is opened before the lock time
' When the sheet is open, before the LockTime, we need to make sure the cells are unlocked
Application.Run "LockSheet"
' Then we can set a timer to run to lock the cells at the defined time.
Application.OnTime UnLockTime - CurrTime + Now, "UnLockSheet"
Else
' This will automaticly Lock the cells if opened after the Lock time but before the unlock time
Application.Run "UnLockSheet"
' If opened after the Lock time but before the unlock time, the timer will need to start, ready to unlock the cells at the specified time
Application.OnTime LockTime - CurrTime + Now, "LockSheet"
End If
End If
End Sub
VBA Code:
Sub LockSheet()
Application.ScreenUpdating = False
' This will lock the cells
' Specify the exact sheet name otherwise whichever sheet is active, will have cells unlocked and a password set
' I would also specify the workbook name to prevent any other workbook cell being locked and sheets being password protected
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A1:Y5").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A7:Y7").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Protect Password:="Password"
MsgBox "Day cells have been locked / Nights unlocked"
' Now the timer has to be reset to unlock the cells
Dim CurrTime As Date
Dim UnLockTime As Date
CurrTime = Time
UnLockTime = TimeValue("18:00:00")
Application.OnTime UnLockTime - CurrTime + Now, "UnLockSheet"
Application.Run "NightUnLock"
Application.ScreenUpdating = True
End Sub
VBA Code:
Sub NightUnLock()
Select Case weekday(Date, vbSunday)
Case 1 'Sunday
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Protect Password:="Password"
Case 2 ' Monday
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Protect Password:="Password"
Case 3 'Tuesday
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Protect Password:="Password"
Case 4 'Wednesday
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Protect Password:="Password"
Case 5 'Thursday
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Protect Password:="Password"
Case 6 'Friday
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Protect Password:="Password"
Case 7 'Saturday
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A6:Y6").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A8:Y8").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Protect Password:="Password"
ThisWorkbook.Save
End Select
End Sub
VBA Code:
Sub UnLockSheet()
Application.ScreenUpdating = False
' This will unlock the cells
' Specify the exact sheet name otherwise whichever sheet is active, will have cells unlocked and a password set
' I would also specify the workbook name to prevent any other workbook cells being unlocked and sheets being password protected
Select Case weekday(Date, vbSunday)
Case 1 'Sunday
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Protect Password:="Password"
Case 2 ' Monday
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Protect Password:="Password"
Case 3 'Tuesday
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Protect Password:="Password"
Case 4 'Wednesday
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Protect Password:="Password"
Case 5 'Thursday
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Protect Password:="Password"
Case 6 'Friday
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Protect Password:="Password"
Case 7 'Saturday
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A1:Y5").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A7:Y7").Locked = False
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Protect Password:="Password"
End Select
Application.Run "NightLock"
ThisWorkbook.Save
MsgBox "Days have been unlocked / Nights Locked"
End Sub
VBA Code:
Sub NightLock()
'Nights Lock
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sun").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Mon").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Tues").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Weds").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Thurs").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Fri").Protect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Unprotect Password:="Password"
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A6:Y6").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Range("A8:Y8").Locked = True
Workbooks("Test - Copy.xlsm").Worksheets("Sat").Protect Password:="Password"
End Sub