解锁excel vba中受保护工作表中的列

时间:2014-01-10 06:52:29

标签: excel vba

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

我正在尝试上面的代码,但没有工作,我想只解锁B和C列

2 个答案:

答案 0 :(得分:4)

在解锁范围之前,您正在保护工作表。但由于纸张受到保护,因此无法解锁范围。交换两行代码将起作用:

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

答案 1 :(得分:0)

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