apply codes to all new sheets added

cybrkada

New Member
Joined
Apr 8, 2014
Messages
42
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Sheets("Sheet1").Range("B3").Value >= -Date And Sheets("Sheet1").Range(" B3").Value <= Date - 1 Then
        ActiveWorkbook.Unprotect Password:="password"
        MsgBox "This workbook is locked, please contact xxx@xxx.com"
    Else
        'MsgBox "unlocked"
    End If
End Sub

Currently the code is working in Sheet1 only. How do I enter it in a module so that it applies to all new sheets added on the workbook?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You can use this event procedure in the ThisWorkbook module:

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

End Sub

Why are you unprotecting the workbook and telling the user that it is protected?
 
Upvote 0
Hi Andrew,

I have already modified code from ActiveWorkbook.Unprotect Password:="password"
to
ActiveWorkbook.Protect Password:="password"

However, after I added a new sheet, B3 on the new sheet still pulls up data on the first sheet. How do I get this fixed?
 
Upvote 0
Try this in the ThisWorkbook module:

Rich (BB code):
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If CLng(Sh.Name) >= CLng(Format(Date, "mmdd")) Then Exit Sub
    Application.EnableEvents = False
    Application.Undo
    Sh.Cells.Locked = True
    Sh.Protect Password:="password"
    MsgBox "This worksheet is locked, please contact xxx@xxx.com"
    Application.EnableEvents = True
End Sub
 
Upvote 0
This gets yellow hightlighted in debug mode whenever i change the value of B3

Code:
If CLng(Sh.Name) >= CLng(Format(Date, "mmdd")) Then
 
Upvote 0
My bad, I am working on a test sheet, ok i will be using now the actual sheet, do I need to modify "Sheet1" by the actual first sheet on MMDD format?
 
Upvote 0
In case one or more sheets aren't named in the format mmdd, add this line at the beginning of the procedure:

Code:
If Not IsNumeric(Sh.Name) Then Exit Sub
 
Upvote 0
Hi Andrew, still doesnt work, by the way in B3 i have =MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256) which pulls up whatever the sheetname is (mmdd). I should not be able to edit cells on any sheet earlier than today's date. However, it does not let me edit cells even on 07/25 which is today's date.
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

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