PDA

View Full Version : Insert Worksheet



Lewej23
02-23-2012, 11:43 AM
I've got a workbook that I created in Excel and I don't want the end users to insert or delete worksheets... I know enough about VBA to tinker around with it a little. But not enough to really know what I'm doing.

My questions:
1. Where do I put all this code? I know to go to the Developer tab and go to View Code, but do I select one of the sheets or the "ThisWorkbook" under the Excel Objects or start a new Module?
2. How is the entire code set up? I know you need to start it out with "Sub" or "Private Sub" and it needs to end with "End Sub", but I don't know exactly what to use.

Thank-you!

D_Marcel
02-23-2012, 07:29 PM
Hi Lewej23, be welcome to VBAX! I'm new here also but gradually we'll learn and help each other. To your proposal, insert a new Module in VBA Editor (ALT+F11) and paste the code bellow:

Sub Workbook_Lock()
If ActiveWorkbook.ProtectStructure Then
ActiveWorkbook.Unprotect
Else
ActiveWorkbook.Protect Structure:=True
End If
End Sub

You can also lock your Module to avoid end users to run the macro and unprotect the Workbook. Right click in the created Module, VBAProject Properties, Protection. when an end user opens the workbook and try to see the code, the password you set will be requested.

Hope it can help you! :thumb
Douglas Marcel

raji2678
02-23-2012, 10:31 PM
If you are not sure how to write a piece of code, record a macro for the same. You can then tweak the auto generated code as per requirements.