+ Reply to Thread
Results 1 to 4 of 4

UnProtecting Sheets Tracking Errors

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    861

    UnProtecting Sheets Tracking Errors

    I'm trying to create a few macros that will password protect all worksheets (not the workbook), and then also be able to unprotect them.

    In working on the unprotect, I want to be able to identify how many worksheets were not unprotected if the wrong password was given. But for some reason, I can not get the msgbox to display at the end of the code to tell how many sheets were not unprotected.

    The below will show a msgbox during the loop. But does not show it at the end?

    Sub UnprotectSheets()
        Dim wsheet As Worksheet
        Dim pword As String
        Dim x As Long
        
        pword = InputBox("Enter Password", "Enter a Password")
        If pword = "" Then
            MsgBox ("No password provided")
            Exit Sub
        Else
            x = 0
            On Error Resume Next
            For Each wsheet In ActiveWorkbook.Worksheets
                wsheet.Unprotect Password:=pword
                If Err.Number <> 0 Then
                    x = x + 1
                     MsgBox ("Incorrect Password")
                End If
             Next wsheet
             MsgBox x & (" worksheets could not be UnProtected")
        End If
        
        On Error GoTo 0
    End Sub
    Edit.......found the typo and fixed. The above code now will process properly.
    Last edited by ptmuldoon; 02-08-2018 at 03:29 PM. Reason: found type

  2. #2
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: UnProtecting Sheets Tracking Errors

    Try:

    MsgBox x & "Worksheets could not be UnProtected"
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  3. #3
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    861

    Re: UnProtecting Sheets Tracking Errors

    Thanks for the help and I think corrected that typo at same time you were posting yesterday. But I'm struggling to get the end result I'm looking for.

    The below code is suppose to loop through the worksheets and when done give 2 message boxes. The first tells the user which sheets were not unlocked (ie, wrong password), and the 2nd message then tells the user which sheets were successfully unlocked. Yet, it keeps telling me ALL were not unlocked when the indeed some were.

    Sub UnprotectSheets()
        Dim wsheet As Worksheet
        Dim pword As String, msg As String, msg2 As String
        
        pword = InputBox("Enter Password", "Enter a Password")
        If pword = "" Then
            MsgBox ("No password provided")
            Exit Sub
        Else
            On Error Resume Next
            For Each wsheet In ActiveWorkbook.Worksheets
                wsheet.Unprotect Password:=pword
                
                If Err.Number <> 0 Then
                     msg = msg & wsheet.Name & vbCrLf
                ElseIf wsheet.ProtectContents = False Then
                    msg2 = msg2 & wsheet.Name & vbCrLf
                End If
                
             Next wsheet
        End If
        
        If Len(msg) > 0 Then
            MsgBox "The following worksheets are protected under a different password and were not unlocked" & _
            vbCrLf & msg, 64, "Notification"
        End If
        
        If Len(msg2) > 0 Then
            MsgBox "The following worksheets were unlocked" & _
            vbCrLf & msg2, 64, "Notification"
        End If
        
        
        
        On Error GoTo 0
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    861

    Re: UnProtecting Sheets Tracking Errors

    Success!!

    I think I need to check if the worksheet is still locked. The below seems to work:

    If wsheet.ProtectContents = True And Err.Number <> 0 Then
                    msg = msg & wsheet.Name & vbCrLf
                ElseIf wsheet.ProtectContents = False Then
                    msg2 = msg2 & wsheet.Name & vbCrLf
                End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help unprotecting sheets
    By Larbec in forum Excel General
    Replies: 1
    Last Post: 12-12-2013, 03:28 PM
  2. protecting/unprotecting sheets
    By Geordie in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-20-2008, 08:57 AM
  3. Unprotecting and Protecting Sheets
    By randolphoralph in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-18-2008, 06:21 PM
  4. Unprotecting Sheets with VBA and IRM
    By Howard in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2006, 12:53 PM
  5. Unprotecting Sheets
    By Darren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2006, 03:25 PM
  6. Tracking Errors
    By Karen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-06-2006, 08:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1