Protect/Unprotect Excel VBA Error

ngann

New Member
Joined
Jan 18, 2018
Messages
21
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top