2013-02-18 99 views
1

我已經鎖定了幾個細胞和工作表,我有工作表宏,但我不能找到一種方法來鎖定工作表,並在同一時間運行的宏。每次我嘗試運行宏時,它被鎖定給我一個錯誤。鎖定Excel工作表具有宏觀

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

您只需在您的宏的開頭

Sheets("D").Unprotect "password"

,並在年底取消保護它

Sheets("D").Protect "password"

+0

重新保護它,它給了我一個錯誤下標超出範圍 – Doolie1106 2013-02-18 15:31:38

+0

嗨,對不起它仍然給我1004錯誤。當我解鎖工作表宏運行良好,但是當我使用上面的代碼來鎖定工作表時,它給了我一個1004錯誤。 – Doolie1106 2013-02-18 15:43:51

+0

你確定它發生在'.Protect'行嗎?如果是這樣,工作表的名稱是否正確? – 2013-02-18 19:31:55