This ws.protect Password = "edc1"
or ws.protect Password := "edc1"
No Work!
it is necessary to send the password by variable, to fix this bug!
Try this one, it worked for me:
For one sheet
Sub Protect()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
Dim strPassword As String: strPassword = "YourPassword"
ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
End Sub
and
Sub UnProtect()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("SHEET")
Dim strPassword As String: strPassword = "YourPassword"
ws.Unprotect Password:=strPassword
End Sub
All Worksheets
Sub ProtectAllSheets()
Dim ws As Worksheet
Dim strPassword As String: strPassword = "YourPassword"
For Each ws In Worksheets
ws.Protect Password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, AllowDeletingRows:=False, _
AllowSorting:=False, AllowFiltering:=False, AllowUsingPivotTables:=False
Next ws
End Sub
and
Sub UnProtectAllSheets()
Dim ws As Worksheet
Dim strPassword As String: strPassword = "YourPassword"
For Each ws In Worksheets
ws.Unprotect Password:=strPassword
Next ws
End Sub