unlock column in protected sheet in excel vba

2019-08-31 01:50发布

Private Sub Worksheet_Activate()
ActiveSheet.Protect "RS"
ActiveSheet.Range("B:C").Locked = False
End Sub

I am trying above code but not working, i want to unlock only B and C column

标签: excel vba
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-31 02:30
Private Sub Workbook_Activate()

Worksheets("ObjectDescriptionMapping").Range("B:C").Locked = False ' unlock the cells, so they can be edited in a protected sheet
Worksheets("ObjectDescriptionMapping").Protect "RS"

End Sub
查看更多
女痞
3楼-- · 2019-08-31 02:51

You are protecting the sheet before you unlock the range. But since the sheet is protected, the range cannot be unlocked. Swap the two lines and the code will work:

Private Sub Worksheet_Activate()
ActiveSheet.Range("B:C").Locked = False ' unlock the cells, so they can be edited in a protected sheet
ActiveSheet.Protect "RS" ' protect the sheet so only unlocked cells can be edited
End Sub
查看更多
登录 后发表回答