?
Solved

ActiveSheet.Unprotect - if password fails in VBA

Posted on 2014-10-07
8
Medium Priority
?
1,316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
8 Comments
 
LVL 26

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 26

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 26

Accepted Solution

by:
ProfessorJimJam earned 2000 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 48

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 26

Expert Comment

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

Expert Comment

by:ProfessorJimJam
ID: 40451013
Author response required
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

771 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