您必须检查启用了错误处理的名称。请参见下面的代码。我也加入了一些其他的变化。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim bValue As String
Dim cValue As String
Dim bIsDayShift As Boolean, bIsAfterShift As Boolean
Dim sMsg As String
Application.EnableEvents = FALSE
sMsg = "This employee has already been assigned for the replaceme shift. "
sMsg = sMsg & "In order to allow this change, this employee's scheduling "
sMsg = sMsg & "for the replaceme shift will be removed."
'Check if cell is required to have both columns with value.
'If it is, skip checks.
On Error Resume Next
bIsDayShift = Target.Name.Name = "DayShift"
bIsAfterShift = Target.Name.Name = "AfterShift"
On Error GoTo 0
If Not bIsDayShift And Not bIsAfterShift Then
'Check if active column is column B.
If Target.Column = 2 Then
If Not IsEmpty(Target.Offset(0, 1).Value) Then
MsgBox Replace(sMsg, "replaceme", "afternoon"), vbExclamation
Target.Offset(0, 1).ClearContents
End If
'Check if active column is column C.
ElseIf Target.Column = 3 Then
If Not IsEmpty(Target.Offset(0, -1).Value) Then
MsgBox Replace(sMsg, "replaceme", "day"), vbExclamation
Target.Offset(0, -1).ClearContents
End If
End If
End If
Application.EnableEvents = TRUE
End Sub