View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
JLatham JLatham is offline
external usenet poster
 
Posts: 2,203
Default how to record macro for passwords

It just takes a little modification to make it all work. I have added the
"masterPassword" constant so that you can control things better. With it,
people will have to always enter the correct password and so the password
won't get changed by accidental (or intentional) typos by users later on.

Naturally, if they look at the code, they'll see the password there, but
it's a step forward. The first line [Const masterPassword = "your!password"]
goes somewhere near the beginning of the code module you put these routines
into, not within either of the Sub routines.

Const masterPassword = "your!password"

Sub ProtectAllSheets()
Dim oSh As Worksheet
Dim thePassword As String

thePassword = _
InputBox("Enter the Password to protect all sheets.", _
"Password Required", "")
If thePassword = masterPassword Then
MsgBox "You did not provide a valid password, cannot continue.", _
vbOKOnly, "No Password Entered"
Exit Sub
End If
For Each oSh In Worksheets
oSh.Protect Password:=thePassword
Next
End Sub

Sub ProtectAllSheets()
Dim oSh As Worksheet
Dim thePassword As String

thePassword = _
InputBox("Enter the Password to unprotect all sheets.", _
"Password Required", "")
If thePassword = masterPassword Then
MsgBox "You did not provide a valid password, cannot continue.", _
vbOKOnly, "No Password Entered"
Exit Sub
End If
For Each oSh In Worksheets
oSh.Unprotect Password:=thePassword
Next
End Sub

"Sherees" wrote:

Hi Jan

Thanks a lot
I have run the macro, n i have done the same macro for unprotecting also,
but i want to give a password for this macro, so when we run this macro, it
will ask for the password so other users cant unprotect the sheet.

Thank you again
--
Life isa journey not a destination


"Jan Karel Pieterse" wrote:

Hi Sherees,

I have more than fifty sheets, which i want to lock with a password? I have
tried to record macro but it does not work as intended. When I run the
recorded macro, it protects the sheet but without the password. Solution
Please!


You can use code like this:

Sub ProtectAllSheets()
Dim oSh As Worksheet
For Each oSh In Worksheets
oSh.Protect "Password"
Next
End Sub

Of course you need to change the password...

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com
Member of:
Professional Office Developer Association
www.proofficedev.com

.