我已经锁定了几个单元格和工作表,我有一个工作表的宏,但我找不到一种方法来锁定工作表并同时运行宏。每次我尝试在锁定时运行宏都会给我一个错误。
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
如何在单元格和工作表被锁定并受到保护的情况下运行宏?
谢谢!
答案 0 :(得分:0)
您只需使用
在宏的开头取消保护即可Sheets("D").Unprotect "password"
最后用
Sheets("D").Protect "password"
重新保护它