christianbiker
Active Member
- Joined
- Feb 3, 2006
- Messages
- 379
Hi all,
I am using the below code when I user would like to change the password for all worksheets in a workbook. Once they enter their current password, all worksheets in the workbook are unprotected with the current password and a second popup appears asking for the new password followed by a third popup that asks for confirmation of the password. If the user clicks "Cancel" or selects the "X" on both new password boxes, the popups close and all worksheets are left unprotected completely. How do I prevent this from happening?
Thanks in advance.
I am using the below code when I user would like to change the password for all worksheets in a workbook. Once they enter their current password, all worksheets in the workbook are unprotected with the current password and a second popup appears asking for the new password followed by a third popup that asks for confirmation of the password. If the user clicks "Cancel" or selects the "X" on both new password boxes, the popups close and all worksheets are left unprotected completely. How do I prevent this from happening?
Thanks in advance.
VBA Code:
MSG1 = MsgBox("***WARNING***" & vbNewLine & vbNewLine & _
"YOU HAVE INITIATED THE PROCESS OF CHANGING THE PASSWORD FOR THE ENTIRE SCHEDULE. DO YOU WISH TO CONTINUE?" & vbNewLine & vbNewLine & _
"Click ""Yes"" to continue, ""No"" to cancel.", vbYesNo, "UNLOCK LIVE SCHEDULE")
If MSG1 = vbYes Then
On Error GoTo booboo
unpass = InputBox("Enter the current password.")
For Each Worksheet In ActiveWorkbook.Worksheets
Worksheet.Unprotect Password:=unpass
Next
GoTo 2
Exit Sub
Else
Exit Sub
End If
Application.ScreenUpdating = False
2:
Dim pwd1 As String, pwd2 As String
pwd1 = InputBox("Please enter the new password.")
If pwd1 = "" Then Exit Sub
pwd2 = InputBox("Re-enter the password." & vbNewLine & vbNewLine & "***ALERT*** PLEASE ENSURE YOU STORE THE NEW PASSWORD IN A SAFE LOCATION." & vbNewLine & vbNewLine & _
"THIS CANNOT BE UNDONE ONCE ""OK"" IS SELECTED.")
If pwd2 = "" Then Exit Sub
'Check if both the passwords are identical
If InStr(1, pwd2, pwd1, 0) = 0 Or _
InStr(1, pwd1, pwd2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken"
Exit Sub
End If
For Each ws In Worksheets
ws.Protect Password:=pwd1, DrawingObjects:=False, Contents:=True, Scenarios:=True, AllowFormattingCells:=True
Next
MsgBox "Your password has been changed and all sheets have been protected."
Worksheets("HOME PAGE").Activate
Exit Sub
Application.ScreenUpdating = True
booboo: MsgBox "You have either cancelled the password change process or there is a problem with your password. Click ""Ok"" to continue."
Exit Sub