Macro won't protect sheet

shiguera

Board Regular
Joined
Feb 7, 2003
Messages
53
Hi every one,

I have a protected sheet and I use a macro to do some calculations. At the beginning of the macro I unprotect the sheet:

ActiveSheet.Unprotect

and it prompts me for the password. Fine so far. At the end of the code I protect the sheet:

ActiveSheet.Protect

The sheet apparently gets protected (I can't edit the cells, which is correct); however, I'm not prompted for the password to protect and if I go to the menu Tools-Protection-Unprotect Sheet... the sheet gets unprotected without asking me for the password. Could any one point out what I'm missing to get the sheet really protected and ask for the password every time I try to unprotect or protec it?

Note: the Workbook is also protected and I do not unprotect it at any time.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You need to tell it to protect using the password ("thePassword" represents the password you want to use in the code below)

Code:
ActiveSheet.Protect "thePassword"
 
Upvote 0
You can set a hard coded password with
Code:
ActiveSheet.Protect "password"

This will invoke the Un/ProtectSheet dialog box
Code:
Application.Dialogs(xlDialogProtectDocument).Show
 
Upvote 0
ActiveSheet.Unprotect ("Your Password here")
Your Code Here
ActiveSheet.Protect ("Your Password here")
 
Upvote 0
Hi guys,

thanks for the replies. I personally like more the "hidden" option so that the password is not written in the code. Tried it and now everything works great!

Thanks again
 
Upvote 0
Hi guys,

thanks for the replies. I personally like more the "hidden" option so that the password is not written in the code. Tried it and now everything works great!

Thanks again
Which option are you refering to?

In the VBE you can goto Tools-->VBA Project Properties-->Protection-->Lock project for viewing-->Set a password. Than your code will not be seen unless you enter the password.

If you don't want your macros to be available in the Tools-->Macro-->Macros dialog, you can preface them with the word Private. I.E. Private Sub Macro1()
 
Upvote 0
Oh! thanks, didn't know that other way around it either.

I was referring to Mikerickson's option:
Application.Dialogs(xlDialogProtectDocument).Show

but I see your way is another way to do it without having the password "exposed".

All great answers guys.
 
Upvote 0
Ok, follow the steps below carefully,
1.When you write the code to protect then you must also supply the password as parameter to the protect function as
ActiveSheet.Protect (“a”)
Now the sheet is protected with password ‘a’
Then if you execute your ActiveSheet.UnProtect code, it will definitely ask you for password
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
Hope you get it
<o:p> </o:p>
Have a nice time….
<o:p> </o:p>
<o:p> </o:p>
Chris
------
Convert your Excel spreadsheet into an online calculator.
http://www.spreadsheetconverter.com
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

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