Protecting a workbook except for a few cells

青春壹個敷衍的年華 提交于 2019-12-13 04:14:35

问题


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!


回答1:


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".



来源:https://stackoverflow.com/questions/51617702/protecting-a-workbook-except-for-a-few-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!