编辑:如果我使用 ActiveSheet.unprotect 代替
好的,如果我的工作表不受保护,以下工作正常!
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Reset_EnableEvents
Application.EnableEvents = False
If Not Intersect(Target, Range("D6:G6")) Is Nothing Then 'do the following if D6 is updated
Range("D7").Interior.ColorIndex = 15
If Range("D6") = "No" Then
Range("D7").Interior.ColorIndex = 38
End If
End If
If Not Intersect(Target, Range("D7:G7")) Is Nothing Then 'do the following if D6 is updated
If Range("D7") <> "" Then
Range("D7").Interior.ColorIndex = 15
End If
End If
If Not Intersect(Target, Range("e12:f12")) Is Nothing Then 'do the following if E12:F12 is updated
Range("d28").Value = Range("e12").Value 'set d28 to date entered in e12
Range("c23").Interior.ColorIndex = 36 ' reset color of cell c23
If Range("e12") = "" Then ' do the following if e12 is empty
Range("c39") = Chr(34) & "Do you work period?" & Chr(34)
Else
Range("c39") = Chr(34) & "Do you work beyond " & Range("e13").Text & " (approx. period)?" & Chr(34)
If Date >= Range("e15").Value Then
Range("c23").Interior.ColorIndex = 3
End If
End If
End If
If Not Intersect(Target, Range("E32")) Is Nothing Then 'do the following if E32 is updated
If Range("e32") = "Yes" Then
MsgBox "Please have the client complete a MVA Questionnaire."
End If
End If
If Not Intersect(Target, Range("E41")) Is Nothing Then 'do the following if E41 is updated
If Range("e41") = "Yes" Then
Range("d42").Interior.ColorIndex = 38
ElseIf Range("e41") = "No" Then
Range("d42").Interior.ColorIndex = 36
Range("d42") = "Not Required"
ElseIf Range("e41") = "" Then
Range("d42").Interior.ColorIndex = 36
Range("d42") = ""
End If
End If
Reset_EnableEvents:
Application.EnableEvents = True
End Sub
但是当我保护工作表并只允许选择未锁定的单元格并向工作表添加密码时,上述单元格背景颜色和单元格值不会更新
我尝试在开头和结尾添加 activeworksheet.unprotect 和protect ,它仍然无法正常工作!
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Reset_EnableEvents
Application.EnableEvents = False
'ActiveWorkSheet.Unprotect Password:="a"
`..... all the if not intersect
ActiveWorkSheet.Protect Password:="a"
Reset_EnableEvents:
Application.EnableEvents = True
End Sub
我还尝试在每个 if 语句中添加保护和取消保护,但它仍然不起作用,
If Not Intersect(Target, Range("e12:f12")) Is Nothing Then 'do the following if E12:F12 is updated
ActiveWorkSheet.Unprotect Password:="a"
Range("d28").Value = Range("e12").Value 'set d28 to date entered in e12
Range("c23").Interior.ColorIndex = 36 ' reset color of cell c23
If Range("e12") = "" Then ' do the following if e12 is empty
Range("c39") = Chr(34) & "Do you work period?" & Chr(34)
Else
Range("c39") = Chr(34) & "Do you work beyond " & Range("e13").Text & " (approx. period)?" & Chr(34)
If Date >= Range("e15").Value Then
Range("c23").Interior.ColorIndex = 3
End If
End If
ActiveWorkSheet.Protect Password:="a"
End If
帮助??