passwords on multiple sheets


Posted by Nick Davy on June 29, 2000 4:41 AM

I have a workbook containing many worksheets which are password protected with the same password, I want to create a macro which will be able to change this password simply.

Any thoughts?

Thanks

Nick

Posted by Ryan on June 29, 0100 8:10 AM

Nick,
Here is code that will do what you want. I've been playing with it and it works like a charm. If there are no passwords, then just leave the first inputbox blank, and enter a password in the second one to protect all the sheets. If you want them all unprotected leave the second inputbox blank. Let me know how it works.
Ryan

Sub ChangePassword()
Dim Sheet As Worksheet
Dim OldPass As Variant
Dim NewPass As Variant

Application.ScreenUpdating = False

OldPass = InputBox("Please enter old password", "Old Password")
'If OldPass = "" Then Exit Sub
NewPass = InputBox("Please enter new password", "New Password")

If MsgBox("This will replace your old password with a new one", vbOKCancel, "Continue") _
= vbCancel Then Exit Sub

For Each Sheet In Worksheets
On Error Resume Next
Sheet.Unprotect Password:=OldPass
If NewPass <> "" Then Sheet.Protect Password:=NewPass
Next Sheet

Application.ScreenUpdating = True
End Sub

Posted by Ryan on June 29, 0100 8:12 AM

On More Thing

The line where it says:

'If OldPass = "" Then Exit Sub

You can delete. Sorry about that, i forgot to take it out.



Posted by Tim on July 11, 0100 5:29 PM

Re: enter the new password TWICE

You ought to force the user to enter the new password twice, and check they're the same before you protect all the sheets

Just in case they didn't hit the keys they intended (or thought the entered)