auto Lock cells after due date

Sophia2

New Member
Joined
Mar 28, 2017
Messages
3
Hello everyone, I have a worksheet with two sheets. one containing the monthly data of personnel

ID#NameJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
SalesMichelle
1257​
5827​
5465​
65451​
51321​
132​
3212​
CostJohn
4254​
5952​
614​
5416​
18545​
3212​
1587​

and another sheet with due date of encoding in each month
MonthDue date
January
8-Feb-23​
February
8-Mar-23​
March
8-Apr-23​
April
9-May-23​
May
9-Jun-23​
June
10-Jul-23​
July
10-Aug-23​
August
10-Sep-23​
September
10-Oct-23​
October
10-Nov-23​
November
10-Dec-23​
December
10-Jan-24​

how to auto lock the column in the data sheet of months with lapsed due date.

for example... today is Sep 8, 2023. therefore, when i open the file, the data columns for January to July should be lock and cannot be edited.
I can only encode or edit in columns for August to December.
On September 11, the august column will also be locked.

I believe a VBA will work for this. But, I am not good with VBA and macro except in recording only.

Thank you in advance for any help on this one.
 

Attachments

  • data.jpg
    data.jpg
    202.8 KB · Views: 16
  • due dates.jpg
    due dates.jpg
    204.6 KB · Views: 17

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi Sophia2,

This can be done with VBA, code below.

This will need to run automatically when the workbook is opened. To do this the code needs to be added to the workbook module, see image. Once saved this will run when the workbook is opened and lock any cells in the monthly columns based on the dates in the 'Due date' column.

To make sure that users are unable to type in the protected cells I have included a password to protect the sheet. However, to make this secure you will need to password protect access to VBA as a user would be ale to access the code to see the password as well!

VBA Code:
Private Sub Workbook_Open()

Dim StrMnth As String
Dim EndMnth As String
Dim DateNow As Date

Sheets("data").Unprotect Password:="SecurePassword!"

With Sheets("data").Range("Table2[ [January]:[December] ]")
.Locked = False
End With

For Each i In Range("Table1[Due Date]")

If i.Value <= Date Then
    If StrMnth = "" Then
    StrMnth = i.Offset(, -1).Value
    Debug.Print "Start " & StrMnth
    End If
    EndMnth = i.Offset(, -1).Value
    Debug.Print "End " & EndMnth
End If

Next i

With Sheets("data").Range("Table2[ [" & StrMnth & "]:[" & EndMnth & "] ]")
.Locked = True
End With

Sheets("data").Protect Password:="SecurePassword!"

End Sub

ThisWorkbook.png


EDIT: PS forgot to add I have the tables named as 'Table1' for your month and due date and 'Table2' is the 'Id', 'Name' and Monthly figures table.

(y)
 
Upvote 1
Solution
Hi Sophia2,

This can be done with VBA, code below.

This will need to run automatically when the workbook is opened. To do this the code needs to be added to the workbook module, see image. Once saved this will run when the workbook is opened and lock any cells in the monthly columns based on the dates in the 'Due date' column.

To make sure that users are unable to type in the protected cells I have included a password to protect the sheet. However, to make this secure you will need to password protect access to VBA as a user would be ale to access the code to see the password as well!

VBA Code:
Private Sub Workbook_Open()

Dim StrMnth As String
Dim EndMnth As String
Dim DateNow As Date

Sheets("data").Unprotect Password:="SecurePassword!"

With Sheets("data").Range("Table2[ [January]:[December] ]")
.Locked = False
End With

For Each i In Range("Table1[Due Date]")

If i.Value <= Date Then
    If StrMnth = "" Then
    StrMnth = i.Offset(, -1).Value
    Debug.Print "Start " & StrMnth
    End If
    EndMnth = i.Offset(, -1).Value
    Debug.Print "End " & EndMnth
End If

Next i

With Sheets("data").Range("Table2[ [" & StrMnth & "]:[" & EndMnth & "] ]")
.Locked = True
End With

Sheets("data").Protect Password:="SecurePassword!"

End Sub

View attachment 98426

EDIT: PS forgot to add I have the tables named as 'Table1' for your month and due date and 'Table2' is the 'Id', 'Name' and Monthly figures table.

(y)
Hi sxhall! thank you so much for the help.
i copied your code and placed it in "thisworkbook" as you illustrated.
but i ran into this error
1694231571647.png


when i run debug
1694231712181.png


i also followed your table names.
1694231777978.png


and the columns month can still be edited.
where did i go wrong?

I really appreciate your response
 
Upvote 0
Hello again sxhall!

i just knew i made a mistake.
my due dates were not formated as dates.
after changing them to date format, your code worked!

thank you so much for the help!!!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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