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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

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