mbake16
02-21-2009, 10:35 AM
I've created a macro which prompts the user for the password to unprotect a sheet. I've added error handling code to loop the macro back to the beginning if the user enters an incorrect password in the inputbox. The macro works fine if the user enters an incorrect password once, however if the user enters an incorrect password during the second pass the macro errors. It seems it ignores the error handling code on the second pass. The code is below, any insight would be great!
Sub unprotect()
Dim password As String
Dim MyErr As String
a:
On Error GoTo b:
password = InputBox("Unprotect")
Sheet1.unprotect password:=password
b:
MyErr = Err.Number
If MyErr <> 0 Then
On Error GoTo 0
MsgBox "Incorrect Password", vbOKOnly
GoTo a:
End If
End Sub
edit lucas: VBA tags added to code. You can format your code for the forum if you select your code and hit the vba button when posting.
Sub unprotect()
Dim password As String
Dim MyErr As String
a:
On Error GoTo b:
password = InputBox("Unprotect")
Sheet1.unprotect password:=password
b:
MyErr = Err.Number
If MyErr <> 0 Then
On Error GoTo 0
MsgBox "Incorrect Password", vbOKOnly
GoTo a:
End If
End Sub
edit lucas: VBA tags added to code. You can format your code for the forum if you select your code and hit the vba button when posting.