locking有macros的Excel工作表

我locking了几个单元格和工作表,我有一个工作表的macros,但我找不到一种方法来locking工作表并同时运行macros。 每当我尝试运行macros时,它被locking给我一个错误。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim g As Long Dim r As Long Dim y As Long Dim overallhealth As Range Dim varaincerange As Range Set varaincerange = Sheets("D").Range("B5:B9") Set overallhealth = Sheets("D").Range("B4") y = Application.WorksheetFunction.CountIf(varaincerange, "y") g = Application.WorksheetFunction.CountIf(varaincerange, "g") r = Application.WorksheetFunction.CountIf(varaincerange, "r") If g = 5 Then overallhealth = "G" ElseIf g = 4 And y = 1 Then overallhealth = "G" ElseIf r >= 2 Then overallhealth = "R" ElseIf y = 1 And r = 1 Then overallhealth = "Y" ElseIf y > 1 And r >= 1 Then overallhealth = "R" ElseIf y = 1 And r > 1 Then overallhealth = "R" ElseIf y >= 3 Then overallhealth = "R" ElseIf g = 3 And y = 2 Then overallhealth = "Y" ElseIf g = 4 And r = 1 Then overallhealth = "Y" End If End Sub 

如何在单元格和工作表被locking和保护的情况下运行macros?

谢谢!

你可以简单地在macros的开始处用保护来解除它

 表格(“D”)取消保护“密码” 

并在最后重新保护它

 表格(“D”)。保护“密码”