protecting sheets
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Protecting sheets

Protecting sheets

resolvedResolved · Low Priority · Version 2003

replyReply Tue 18 Oct 2011, 16:41Delegate David said...

David has attended:
Excel Intermediate course
Excel Advanced course

Protecting sheets

How do you automatically 'protect' a sheet that has a password on when it is closed down. so that if the password has been taken off when the sheet was opened (tools - protection - unprotect sheet) - when you close & reopen the sheet it is protected?

For upcoming training course dates see: Pricing & availability

replyReply Wed 19 Oct 2011, 10:32Trainer Simon said...

RE: Protecting sheets

Hi David,

Thank you for your question. This question is outside the scope of the forum but I am happy to give you the code and an explanation.

You need some VBA code to achieve this:

Dim shtAnySheet As Worksheet
Dim sPassword As String
sPassword = "dog"
Set shtAnySheet = Worksheets("Sheet1")
shtAnySheet.Protect sPassword
End Sub

The code above protects the sheet in the active workbook called Sheet1, with a password of dog. As the code has been entered into the Workbook_BeforeClose event, then this code will run each time the workbook is closed.

Open your workbook and use ALT + F11 to open the VBA window. Make sure you have the Project Explorer window open(side window with VBA Projects listed).

Find the VBA project relating to your workbook and expand it out. Double click on 'This Workbook'. In the right hand window that appears, from the left drop down box that says 'General', select Workbook. From the second drop down on the right hand side choose the 'BeforeClose' Event.

This should create a sub procedure with end sub at the end(Private Sub Workbook_BeforeClose(Cancel As Boolean)). Copy the code in this email and paste it in between the Private Sub and End sub lines. Then close and save Excel. You must save the workbook using Save As and save it as a Macro Enabled workbook. When you open the file, you must click Enable content for the macro to run. Obviously, replace the word dog with your own password.

I have attached a screenshot showing the VBA code and window.

I hope this answers your question.

Regards

Simon

Attached files...

VBA Screenshot.doc

replyReply Wed 26 Oct 2011, 16:53Delegate David said...

RE: Protecting sheets

Thanks for the answer - it has solved the problem - is it possible though to allow user's to use the auto filter when the sheet is locked other than by ticking the allow auto filter box when manually protecting the sheet?

replyReply Fri 28 Oct 2011, 16:19Trainer Simon said...

RE: Protecting sheets

Hi David,

Thank you for your response.

This would require some more VBA code being developed which will be outside the scope of the forum.

However please find the following links that may help you build the code.

http://www.ozgrid.com/VBA/autofilter-vba.htm
http://www.contextures.com/xlautofilter03.html

I hope this helps.

Regards

Simon

replyReply Mon 31 Oct 2011, 16:32Delegate David said...

RE: Protecting sheets

Thanks Simon

I've now got the spreadsheet proteced but users can use autofilter by using the example VBA codes on the websites

David

 

Please browse our web site to find out more about
excel training courses london and other Microsoft training courses.

Excel tip:

Printing spreadsheets without opening them first

Here's a fast way of printing a spreadsheet from Windows Explorer/My Computer.

Go to the location where the spreadsheet is saved on your computer, then right-click on the icon next to the document and select Print from the menu.

The spreadsheet will automatically open, print and close itself.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard