Locking Sheet after Particular Date

Bavliya

New Member
Joined
Jun 28, 2024
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone

can you help me for Locking Sheet after Particular Date.

I have Excel file which have 13 sheet, first sheet is Master Sheet and rest of it has represent 12 month, in each sheet has monthly data. I want to Lock current sheet after 8 of next month. Rest of it will be remain unlock till his Lock date.

Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi

Do you mean your workbook looks something like this, and for example the tab marked June you want to lock after July 8?

1719814042211.png
 
Upvote 0
Read the comments carefully. Where they are in between <<<< comment >>>> you need to do something


This code needs to go into the Workbook codesheet. Open the VBA editor, in the left panel you will see the workbook with all the sheets listed. The last item in the list is called Workbook. click on this to open its codesheet. Now paste the following in there.

It will run each time the workbook is opened.


VBA Code:
Option Explicit

Private Sub Workbook_Open()
   
    'lock sheets if necessary
    LockAll
End Sub


Private Sub LockAll()
    Dim wsWS As Worksheet
    Dim rDate As Range
    Dim dtNow As Date
    Dim vMonths As Variant
    Dim iDay As Integer, iMonth As Integer, iWBYear As Integer, lR As Long
    Dim bProtect As Boolean
   
    Const sPW As String = "MyPassword"  '<<<< to be modified >>>>
    Const sMONTHS As String = "january,february,march,april,may,june,july,august,september,october,november,december" '<< note: All lower case
   
    'get year for the workbook - assuming it is in Master Sheet in some cell. <<<< Ammend as required >>>>
    iWBYear = Sheets("Master Sheet").Range("A1")
   
    'store todays day and month number
    iDay = Day(Date)
    iMonth = Month(Date)
   
    ' make an array with each of the month names
    vMonths = Split(sMONTHS, ",")
   
    'loop through each of the sheets
    For Each wsWS In Me.Sheets
        If Not wsWS.Name Like "Master Sheet" Then
            ' Assumes month is in cell A1 on each sheet. Modify address as required
            Set rDate = wsWS.Range("A1")    '<<<< modify address to suit >>>>
           
            'now check if the sheet should be locked - after the 8th of the next month
            For lR = 0 To 11    'vMonths is an array starting from 0
                If Trim(LCase(rDate.Text)) Like vMonths(lR) Then
                    Exit For
                End If
            Next lR
            lR = lR + 1     'lR now has the month number of the sheet
           
            Select Case lR
                Case 13
                    'error, sheet cell A1 does not contain valid month
                    MsgBox prompt:="Sheet " & wsws.name & " does not contain a valid month name in" & vbCrLf & _
                                    rDate.Address & ". Please check the sheets so each has the month's name in this cell", _
                                    Title:="Error: Month name not found in sheet"
                Case 12
                    'December - needs to link to next year January
                    If Date > DateSerial(iWBYear, 1, 7) Then
                        bProtect = True
                    End If
                Case Else
                    ' all other months
                    If (iMonth = lR + 1 And iDay > 7) Or iMonth > lR + 1 Then
                        bProtect = True
                    End If
            End Select
            If bProtect Then
                wsWS.Protect sPW, _
                            AllowSorting:=True, _
                            AllowFiltering:=True, _
                            AllowUsingPivotTables:=True
                bProtect = False
            End If
        End If
    Next wsWS
End Sub
 
Upvote 0
Read the comments carefully. Where they are in between <<<< comment >>>> you need to do something


This code needs to go into the Workbook codesheet. Open the VBA editor, in the left panel you will see the workbook with all the sheets listed. The last item in the list is called Workbook. click on this to open its codesheet. Now paste the following in there.

It will run each time the workbook is opened.


VBA Code:
Option Explicit

Private Sub Workbook_Open()
  
    'lock sheets if necessary
    LockAll
End Sub


Private Sub LockAll()
    Dim wsWS As Worksheet
    Dim rDate As Range
    Dim dtNow As Date
    Dim vMonths As Variant
    Dim iDay As Integer, iMonth As Integer, iWBYear As Integer, lR As Long
    Dim bProtect As Boolean
  
    Const sPW As String = "MyPassword"  '<<<< to be modified >>>>
    Const sMONTHS As String = "january,february,march,april,may,june,july,august,september,october,november,december" '<< note: All lower case
  
    'get year for the workbook - assuming it is in Master Sheet in some cell. <<<< Ammend as required >>>>
    iWBYear = Sheets("Master Sheet").Range("A1")
  
    'store todays day and month number
    iDay = Day(Date)
    iMonth = Month(Date)
  
    ' make an array with each of the month names
    vMonths = Split(sMONTHS, ",")
  
    'loop through each of the sheets
    For Each wsWS In Me.Sheets
        If Not wsWS.Name Like "Master Sheet" Then
            ' Assumes month is in cell A1 on each sheet. Modify address as required
            Set rDate = wsWS.Range("A1")    '<<<< modify address to suit >>>>
          
            'now check if the sheet should be locked - after the 8th of the next month
            For lR = 0 To 11    'vMonths is an array starting from 0
                If Trim(LCase(rDate.Text)) Like vMonths(lR) Then
                    Exit For
                End If
            Next lR
            lR = lR + 1     'lR now has the month number of the sheet
          
            Select Case lR
                Case 13
                    'error, sheet cell A1 does not contain valid month
                    MsgBox prompt:="Sheet " & wsws.name & " does not contain a valid month name in" & vbCrLf & _
                                    rDate.Address & ". Please check the sheets so each has the month's name in this cell", _
                                    Title:="Error: Month name not found in sheet"
                Case 12
                    'December - needs to link to next year January
                    If Date > DateSerial(iWBYear, 1, 7) Then
                        bProtect = True
                    End If
                Case Else
                    ' all other months
                    If (iMonth = lR + 1 And iDay > 7) Or iMonth > lR + 1 Then
                        bProtect = True
                    End If
            End Select
            If bProtect Then
                wsWS.Protect sPW, _
                            AllowSorting:=True, _
                            AllowFiltering:=True, _
                            AllowUsingPivotTables:=True
                bProtect = False
            End If
        End If
    Next wsWS
End Sub
i just try code but it's show error like "subscript out of range"
1719982329585.png
 
Upvote 0
See if this works for you. It needs to go in the code for 'ThisWorkbook'

Excel Formula:
Private Sub Workbook_Open()
    Dim ws As Worksheet, sheetDate As Date
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Master" Then
            sheetDate = DateValue("08-" & ws.Name)
            If Date > sheetDate Then
                ' If you want to use a password uncomment line below and change "pwd" to what you want.
                'ws.Protect Password:="pwd"
                ' If you don't need a password use line below
                ws.Protect
            End If
        End If
    Next ws
End Sub
 
Upvote 0
See if this works for you. It needs to go in the code for 'ThisWorkbook'

Excel Formula:
Private Sub Workbook_Open()
    Dim ws As Worksheet, sheetDate As Date
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Master" Then
            sheetDate = DateValue("08-" & ws.Name)
            If Date > sheetDate Then
                ' If you want to use a password uncomment line below and change "pwd" to what you want.
                'ws.Protect Password:="pwd"
                ' If you don't need a password use line below
                ws.Protect
            End If
        End If
    Next ws
End Sub
after 8th of july it's lock july sheet.
july sheet lock after the 8th of aug.
 
Upvote 0
Here's an amendment:

VBA Code:
Private Sub Workbook_Open
    Dim ws As Worksheet, sheetDate As Date, NextSheetdate As Date
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Master" Then
            sheetDate = DateValue("08-" & ws.Name)
            NextSheetdate = DateSerial(Year(sheetDate), Month(sheetDate) + 1, Day(sheetDate))
            If Date > NextSheetdate Then
                ' If you want to use a password uncomment line below and change "pwd" to what you want.
                'ws.Protect Password:="pwd"
                ' If you don't need a password use line below
                ws.Protect
            End If
        End If
    Next ws
End Sub
 
Upvote 1
Here is a version, checked, and I have added option to use sheet names. See comments
VBA Code:
Option Explicit

Private Sub Workbook_Open()
   
    'lock sheets if necessary
    LockAll
End Sub


Private Sub LockAll()
    Dim wsWS As Worksheet
    Dim rDate As Range
    Dim dtNow As Date
    Dim vMonths As Variant
    Dim iDay As Integer, iMonth As Integer, iWBYear As Integer, lR As Long
    Dim bProtect As Boolean
    Dim sMonth As String
   
    Const sPW As String = "MyPassword"  '<<<< to be modified >>>>
    Const sMONTHS As String = "january,february,march,april,may,june,july,august,september,october,november,december" '<< note: All lower case
   
    'get year for the workbook - assuming it is in Master Sheet in some cell. <<<< Ammend as required >>>>
    iWBYear = Sheets("Master Sheet").Range("A1")
   
    'store todays day and month number
    iDay = Day(Date)
    iMonth = Month(Date)
   
    ' make an array with each of the month names
    vMonths = Split(sMONTHS, ",")
   
    'loop through each of the sheets
    For Each wsWS In Me.Sheets
        If Not wsWS.Name Like "Master Sheet" Then
' <<<< there are two options here. Comment out the one not to be used >>>>
        'Option 1 - month name on each sheet
            ' Assumes month is in cell A1 on each sheet. Modify address as required
            Set rDate = wsWS.Range("A1")   '<<<< modify address to suit >>>>
            sMonth = LCase(rDate.Text)
        'Option 2  - Month name on the tabs
            ' alternative: check tab names
            sMonth = LCase(wsWS.Name)
'<<<>>>
            'now check if the sheet should be locked - after the 8th of the next month
            For lR = 0 To 11    'vMonths is an array starting from 0
                If Trim(sMonth) Like vMonths(lR) Then
                    Exit For
                End If
            Next lR
            lR = lR + 1     'lR now has the month number of the sheet
           
            Select Case lR
                Case 13
                    'error, sheet cell A1 does not contain valid month
                    MsgBox prompt:="Sheet " & wsWS.Name & " does not contain a valid month name in" & vbCrLf & _
                                    rDate.Address & " or as tabname. Please check the sheets so each has the month's name in this cell", _
                                    Title:="Error: Month name not found in sheet"
                Case 12
                    'December - needs to link to next year January
                    If Date > DateSerial(iWBYear, 1, 7) Then
                        bProtect = True
                    End If
                Case Else
                    ' all other months
                    If (iMonth = lR + 1 And iDay > 7) Or iMonth > lR + 1 Then
                        bProtect = True
                    End If
            End Select
            If bProtect Then
                wsWS.Protect sPW, _
                            AllowSorting:=True, _
                            AllowFiltering:=True, _
                            AllowUsingPivotTables:=True
                bProtect = False
            End If
        End If
    Next wsWS
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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