Solved

ActiveSheet.Unprotect - if password fails in VBA

Posted on 2014-10-07
8
815 Views
Last Modified: 2014-11-18
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
Comment
Question by:gisvpn
  • 5
8 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40366816
Put this before

On error resume next


Then after the password VBA line put
On error go to 0
0
 

Author Comment

by:gisvpn
ID: 40366820
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
 
LVL 25

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 500 total points
ID: 40366935
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 25

Accepted Solution

by:
ProfessorJimJam earned 500 total points
ID: 40366948
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40451012
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40451011
The author need to provide on the last comment/solution provided to him
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40451013
Author response required
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now