Search code examples
excelvbaspreadsheet-protection

Worksheet protection set using ws.protect - but doesnt unprotect using the Menu (Review - Unprotect sheet)


I have the following code to protect the sheets of a workbook in Excel 2007

Private Sub password_protectallsheets()
For Each ws In Worksheets
ws.protect Password = "edc1"
Next
End Sub

but when I try to unprotect the sheet using the password through the Excel 2007 Menu (review -> Unprotect sheet), it says the password you have supplied is not correct.

Any help is hugely appreciated.


Solution

  • Try:

    Option Explicit
    
    Private Sub password_protectallsheets()
        Dim ws As Worksheet
        For Each ws In Worksheets
            ws.Protect "edc1"
        Next
    End Sub
    

    without the password key argument.
    It worked on my Excel 2007.

    Btw, be sure you copy/paste the password or be sure to check if the last character is wether a 1 (one) or a l (lower-case letter L).