Protect/Unprotect Excel VBA Error

ngann

New Member
Joined
Jan 18, 2018
Messages
18
Can anyone tell me why this is not working correctly? I can protect all the sheets but when I go to unprotect the sheets I get the error message?

Sub protectsheets()
Dim wsheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Password:", "Protect Sheets")
For Each wsheet In ActiveWorkbook.Worksheets
wsheet.Protect Password:=Pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True
Next wsheet
End Sub


Sub unprotectsheets()
Dim wsheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Password:", "Unprotect Sheets")
On Error Resume Next
For Each wsheet In Worksheets
wsheet.Unprotect Password:=Pwd
Next wsheet
If Err <> 0 Then
MsgBox "You have entered an incorrect password. All worksheets could not " & "be unprotected.", vbCritical, "Incorrect Password"
End If
On Error GoTo 0
End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,305
The password you enter is case-sensitive, so make sure that you enter your password in the proper case. Also, your code works on the active workbook, so make sure that the target workbook is in fact the active workbook before running your code.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,033
Messages
5,526,362
Members
409,697
Latest member
christopherlewis1620

This Week's Hot Topics

Top