Shriram Sapate
Shriram Sapate

Reputation: 175

unlock column in protected sheet in excel vba

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

Upvotes: 1

Views: 10915

Answers (2)

Shriram Sapate
Shriram Sapate

Reputation: 175

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

Upvotes: 0

teylyn
teylyn

Reputation: 35905

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

Upvotes: 4

Related Questions