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?
Edit.......found the typo and fixed. The above code now will process properly.![]()
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
Bookmarks