ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code to Prevent Adding Worksheets (https://www.excelbanter.com/excel-programming/323711-code-prevent-adding-worksheets.html)

Paige

Code to Prevent Adding Worksheets
 
I am trying to prevent a user from adding worksheets to a particular
workbook. The code I am using (see below) doesn't work; I don't get any
error messages. Any suggesions on what I am doing wrong?

Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
MsgBox "New sheets may not be added. " & _
"Sheet will be deleted"
Sh.Delete
Application.DisplayAlerts = True
End Sub

Thanks...Paige

Ron de Bruin

Code to Prevent Adding Worksheets
 
Hi Paige

Why don't you protect the workbook


--
Regards Ron de Bruin
http://www.rondebruin.nl



"Paige" wrote in message ...
I am trying to prevent a user from adding worksheets to a particular
workbook. The code I am using (see below) doesn't work; I don't get any
error messages. Any suggesions on what I am doing wrong?

Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
MsgBox "New sheets may not be added. " & _
"Sheet will be deleted"
Sh.Delete
Application.DisplayAlerts = True
End Sub

Thanks...Paige




Rob Bovey

Code to Prevent Adding Worksheets
 
"Paige" wrote in message
...
I am trying to prevent a user from adding worksheets to a particular
workbook. The code I am using (see below) doesn't work; I don't get any
error messages. Any suggesions on what I am doing wrong?

Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
MsgBox "New sheets may not be added. " & _
"Sheet will be deleted"
Sh.Delete
Application.DisplayAlerts = True
End Sub


Hi Paige,

Your code seems to work OK for me here. But wouldn't it be easier to
just protect the workbook? That would prevent the user from adding sheets
without any code at all.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm



Myrna Larson

Code to Prevent Adding Worksheets
 
Hmmm.... your code works fine for me. What do you mean when you say it doesn't
work? Do not see the message box? If so, are you sure that events are enabled?
You could add the line

Application.EnableEvents = True

to the Workbook_Open event code.

But have you considered protecting the workbook's structure? That disallows
adding sheets (and possibly some other things). Check it out in Help.

On Mon, 21 Feb 2005 13:01:04 -0800, "Paige"
wrote:

I am trying to prevent a user from adding worksheets to a particular
workbook. The code I am using (see below) doesn't work; I don't get any
error messages. Any suggesions on what I am doing wrong?

Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
MsgBox "New sheets may not be added. " & _
"Sheet will be deleted"
Sh.Delete
Application.DisplayAlerts = True
End Sub

Thanks...Paige




All times are GMT +1. The time now is 04:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com