Lock a worksheet after certain date is passed

mr names

New Member
Joined
Feb 11, 2011
Messages
14
Hi there

I'm trying to find a macro that I can place into a worksheet to lock it for editing after a certain date passes. I could do with them being able to view it still. I already have the sheet protected as I didn't want people the edit the layout so if there is another way other than using the 'protect sheet' method, that would be great!

The date will be driven from cell $B$3 but I would like it to be that date plus 5 days for example?

Could also do with the message that comes up being tailored in some way to have the contact details of the person that is required to unlock it?

Hope this is enough information...

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi there,

Maybe this will get you started?

Code:
Private Sub Workbook_Open()
    If Sheets(1).Cells(1, 1).Value >= Date And Sheets(1).Cells(1, 1).Value <= Date + 5 Then
        ActiveWorkbook.Protect Password:="password"
        MsgBox "This workbook is locked, please contact xxx@xxx.com"
    Else
        'MsgBox "unlocked"
    End If
End Sub
Change the sheets(1).cells(1,1) to whatever sheet and cell your date is in.
 
Upvote 0
Hi and thanks!

Just tried it and seem to be getting an application error.

I did change it to only apply to one worksheet (sheet7) so not sure if that has made the code work incorrectly?

Hope you can help!

I'm new to excel so a bit of a novice :)
 
Upvote 0
What is sheet7 called? Is it the 7th sheet in your workbook? Sheets(7) will apply to whatever excel thinks is your 7th sheet - if you add 7 sheets, delete the 7th and add a new one, the new one will be sheet 8.

Try: Sheets("Sheet7") instead - change Sheet7 to whatever your sheet is called.
 
Upvote 0
hmmm - just tried that and it comes up with runtime error 13; type mismatch. This is the code that I have so far (as I said, I'm a novice so if it's bad - I apologise!). Sheet 7 is named "Gate 2"...but i've tried this and the same error occurs:



Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Sheets(7).Cells("B3").Value >= Date And Sheets(7).Cells("B3").Value <= Date + 5 Then
ActiveWorkbook.Protect Password:="password"
MsgBox "This workbook is locked, please contact xxx@xxx.com"
Else
'MsgBox "unlocked"
End If
End Sub
 
Upvote 0
Try:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Sheets("Gate 2").Range("B3").Value >= Date And Sheets("Gate 2").Range("B3").Value <= Date + 5 Then
        ActiveWorkbook.Protect Password:="password"
        MsgBox "This workbook is locked, please contact xxx@xxx.com"
    Else
        'MsgBox "unlocked"
    End If
End Sub
 
Upvote 0
Thats strange - not getting an error anymore but the code doesn't seem to be doing anything?

It's not locking or displaying a message.:eeek:
 
Upvote 0
Hi,

Because the code is in "Worksheet_SelectionChange" it will only work in the sheet you have put the code in, that's why I suggested Workbook_Open.
 
Upvote 0
I see....But the code wont protect the sheet that it is assigned too either?

I have put the date in B3 in the past (ie 10 days ago) so therefore it should see this as being more than the 5 days and lock the sheet with a message box shouldn't it. As it is at the moment, the sheet is still fully editable and doesn't give a message box?

As I said, I'm a newbie so sorry if it's me thats causing this!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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