• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1688
  • Last Modified:

ActiveSheet.Unprotect - if password fails in VBA

Hello,

I have in my code the unprotecting of a worksheet (which is password protected). If the user enters the wrong password (or none at all) how can I capture this error and gracefully exit the macro? Is there an onerror or something that can be used here?

Regards,

GISVPN
0
gisvpn
Asked:
gisvpn
  • 5
2 Solutions
 
ProfessorJimJamCommented:
Put this before

On error resume next


Then after the password VBA line put
On error go to 0
0
 
gisvpnAuthor Commented:
Hi ProfessorJimJam,

Thanks for the post - what does this do (would this exit the code) - Could I also add an MsgBox with a message on exit from the code?
0
 
ProfessorJimJamCommented:
yes, it jumps to the error handler and subsequently ends the procedure.   see the example below


Private Sub test()
 On Error goto Message
 ActiveSheet.Unprotect
 Message:
 MsgBox("Wrong password entered")
 End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ProfessorJimJamCommented:
I am not sure, how your code looks like.  

but the simple code to prompt for password and if it is incorrect the gives the message box then the following code would be useful.

Sub UnProtectAllworksheets() 
     
    Dim wSheet          As Worksheet 
    Dim Pwd             As String 
     
    Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input") 
    On Error Resume Next 
    For Each wSheet In Worksheets 
        wSheet.Unprotect Password:=Pwd 
    Next wSheet 
    If Err <> 0 Then 
        MsgBox "You have entered an incorect password. All worksheets could not " & _ 
        "be unprotected.", vbCritical, "Incorect Password" 
    End If 
    On Error Goto 0 
     
End Sub 

Open in new window

0
 
Martin LissRetired ProgrammerCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
ProfessorJimJamCommented:
The author need to provide on the last comment/solution provided to him
0
 
ProfessorJimJamCommented:
Author response required
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now