protecting sheets

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

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

Protecting sheets

resolvedResolved · Low Priority · Version 2003

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?

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

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?

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

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


 

Excel tip:

Display pictures on Chart Data Point

Replacing a single chart data point bar with a picture.
Step 1: Left click on a bar. Then, wait, and do a second single click on the bar. This will select just one data point.

Step 2: Right click on the bar and select Format Data Point.

Step 3: On the fill effects tab, choose a picture. Browse for a picture for that bar. Indicate if you want it to be stretched or stacked. Repeat for each bar.

View all Excel hints and tips


Server loaded in 0.09 secs.