How do I lock a worksheet and stop data entry after a certain date and time?

andyamcconnell

New Member
Joined
May 23, 2024
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hi,
I have a spread sheet for managers to enter the hours worked by staff each week. The spreadsheet has a separate work sheet for every week (52 sheets). The spread sheet is used to calculate staff pay and after the week is over I no longer want managers to change the input data. Is this possible?

The sheet is currently protected apart from the high lighted grey areas, which allow the managers to input hours (although these cells do have data Validation to allow only certain data to be entered).

In the attached example, worksheet "week 27" the sheet is dated week commencing Monday 29/04/2024. Ideally I would like data to be entered up until 12.00 the following Monday (6/05/2024) and after that it will become locked. "Week 28" will be from week commencing Monday 6/05/2024 and it will ideally close on Monday 13/05/2024 at 12.00 and so on.

I have been googling around trying to figure this out for a while, but have had no success, and it's driving me knots. So any help would be greatly appreciated.

Andy
 

Attachments

  • Staff Planner.jpg
    Staff Planner.jpg
    235.8 KB · Views: 7

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Ok, so there's a lot going on here. I created a reference table for the Week sheets and the expiration for each on a sheet Called "SETUP". Change the year and the week dates follow. The code needs to be placed in THISWORKBOOK module in VBA. Every time a user opens the workbook it checks each sheet expiration date and time and does one of two things.
* If the expiration is before today's date at noon it then locks all cells on the sheet
* If the expiration date/time is after today's date at noon it then unlocks a range on every Week sheet called "EditRng"

You will need to create a named range on every Week sheet called EditRng that includes the cells you want the user to edit in the course of finishing the timesheet. The Named Ranges need to be a Worksheet Scope. You also need to create a Named Range called ExpireRng on the Setup. The range for this should be the column of Expiration Dates not including the header.

1716496528721.png


VBA Code:
Private Sub Workbook_Open()
  Dim Cel As Range
  Dim ExpireRng As Range
  Dim Dt As Date
  Dim ShtName As String
  Dim Sht As Worksheet
  
  Set ExpireRng = Sheets("SETUP").Range("ExpireRng")
  
  For Each Cel In ExpireRng
    Dt = Cel.Value
    If Dt < Now Then
      ShtName = Cel.Offset(0, -1).Value
      If IsSheet(ShtName) Then
        Set Sht = Sheets(ShtName)
        Sht.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
          , AllowFormattingCells:=True, UserInterfaceOnly:=True
        Sht.Cells.Locked = True
      End If
    Else
      ShtName = Cel.Offset(0, -1).Value
      If IsSheet(ShtName) Then
        Set Sht = Sheets(ShtName)
        Sht.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
          , AllowFormattingCells:=True, UserInterfaceOnly:=True
        Sht.Range("EditRng").Locked = False
      End If
    End If
  Next Cel

End Sub


Function IsSheet(ShtName As String) As Boolean
  Dim Sht As Worksheet
  On Error Resume Next
  Set Sht = Sheets(ShtName)
  On Error GoTo 0
  If Not Sht Is Nothing Then IsSheet = True
  
End Function

BookAAAAA 20240523.xlsm
CD
1Year:2024
2SheetExpiration
3Week 01Mon 1/8/2024 12:00
4Week 02Mon 1/15/2024 12:00
5Week 03Mon 1/22/2024 12:00
6Week 04Mon 1/29/2024 12:00
7Week 05Mon 2/5/2024 12:00
8Week 06Mon 2/12/2024 12:00
9Week 07Mon 2/19/2024 12:00
10Week 08Mon 2/26/2024 12:00
11Week 09Mon 3/4/2024 12:00
12Week 10Mon 3/11/2024 12:00
13Week 11Mon 3/18/2024 12:00
14Week 12Mon 3/25/2024 12:00
15Week 13Mon 4/1/2024 12:00
16Week 14Mon 4/8/2024 12:00
17Week 15Mon 4/15/2024 12:00
18Week 16Mon 4/22/2024 12:00
19Week 17Mon 4/29/2024 12:00
20Week 18Mon 5/6/2024 12:00
21Week 19Mon 5/13/2024 12:00
22Week 20Mon 5/20/2024 12:00
23Week 21Mon 5/27/2024 12:00
24Week 22Mon 6/3/2024 12:00
25Week 23Mon 6/10/2024 12:00
26Week 24Mon 6/17/2024 12:00
27Week 25Mon 6/24/2024 12:00
28Week 26Mon 7/1/2024 12:00
29Week 27Mon 7/8/2024 12:00
30Week 28Mon 7/15/2024 12:00
31Week 29Mon 7/22/2024 12:00
32Week 30Mon 7/29/2024 12:00
33Week 31Mon 8/5/2024 12:00
34Week 32Mon 8/12/2024 12:00
35Week 33Mon 8/19/2024 12:00
36Week 34Mon 8/26/2024 12:00
37Week 35Mon 9/2/2024 12:00
38Week 36Mon 9/9/2024 12:00
39Week 37Mon 9/16/2024 12:00
40Week 38Mon 9/23/2024 12:00
41Week 39Mon 9/30/2024 12:00
42Week 40Mon 10/7/2024 12:00
43Week 41Mon 10/14/2024 12:00
44Week 42Mon 10/21/2024 12:00
45Week 43Mon 10/28/2024 12:00
46Week 44Mon 11/4/2024 12:00
47Week 45Mon 11/11/2024 12:00
48Week 46Mon 11/18/2024 12:00
49Week 47Mon 11/25/2024 12:00
50Week 48Mon 12/2/2024 12:00
51Week 49Mon 12/9/2024 12:00
52Week 50Mon 12/16/2024 12:00
53Week 51Mon 12/23/2024 12:00
54Week 52Mon 12/30/2024 12:00
SETUP
Cell Formulas
RangeFormula
D3D3=IF(WEEKDAY(DATE($D$1,1,1)-1,2)=7,DATE($D$1,1,1)+7,8-WEEKDAY(DATE($D$1,1,1),2)+DATE($D$1,1,1))+0.5
D4:D54D4=D3+7
Named Ranges
NameRefers ToCells
ExpireRng=SETUP!$D$3:$D$54D4
 
Upvote 0

Forum statistics

Threads
1,217,370
Messages
6,136,154
Members
449,994
Latest member
Rocky Mountain High

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top