Ellie
Ellie

Reputation: 43

Protecting a workbook except for a few cells

I am trying to protect my workbook but leave a few cells unprotected for user inputs. The code I am trying is as follows:

Private Sub Workbook_Open()

ActiveWorkbook.Sheets("User Inputs").Range("D6:D12").Locked = False

Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    ws.Protect Password = Password
Next ws

End Sub

When I run this, I get an error that says it is unable to set the locked property of the range class. How can I fix this? Also, when I try to set a password manually and then try to unprotect with a macro, it says the password is incorrect, even if I have entered it. Do you know why this might be?

Thanks! I really appreciate any help!

Upvotes: 0

Views: 584

Answers (1)

Vityata
Vityata

Reputation: 43585

When you are explicitly giving the parameter by name, you should use the := operator:

Private Sub Workbook_Open()

    ActiveWorkbook.Sheets("User Inputs").Range("D6:D12").Locked = False

    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
         ws.Protect Password := "SomePassword"
    Next ws

End Sub

As an alternative, simply write ws.Protect "SomePassword".

Upvotes: 2

Related Questions