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 Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I add new sheets by copying the previous sheet and just deleting values on some columns. This also copies whatever the code is in the previous sheet. Maybe it's the culprit. Can this be fixed?
 
Upvote 0
Our standard process is by copying the previous sheet, this retains formatting, we dont want copy pasting these into new cells. Are there other options Andrew?
 
Upvote 0
I see, how about this error message "unable to set the locked property of the range class"? excel did not let me edit cells on yesterday's sheet but column B is editable then throws the said error message.
 
Upvote 0
In that case you shouldn't be able to edit the cell, so the Change event won't fire.

The code assumes that initially your worksheets are unprotected. If you try to change data on a worksheet whose name is less than today, your action will be undone, all cells will be locked and the worksheet will be protected. Thereafter, Excel will prevent you from changing any cells. You shouldn't see the error "unable to set the locked property of the range class".
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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