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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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