在一个工作表中运行多个“Private Sub Worksheet_Change(ByVal Target As Range)”

我试图把2个代码合并成一个Private Sub,而第一个代码运行正常,第二个代码根本没有被接收。 它不会返回任何错误,只是不调用所需的Sub。 任何帮助将不胜感激。

Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo justenditall Application.EnableEvents = False If Not Intersect(Target, Range("e6:e1000, M6:m1000")) Is Nothing Then If Target.Value <> "" Then ActiveSheet.Unprotect Password:="password" Target.Locked = True ActiveSheet.Protect Password:="password" End If Next ElseIf Not Intersect(Target, Range("P1")) Is Nothing Then If Target.Value = 1 Then Call SetRecipients End If Next justenditall: Application.EnableEvents = True End Sub 

你的代码有Next ,这是不需要的。 而你是缺lessEnd If 。 我感到惊讶的是代码运行在所有执行第一个IF / ENDIF

这适用于我( 试验和testing

 Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo justenditall Application.EnableEvents = False If Not Intersect(Target, Range("e6:e1000, M6:m1000")) Is Nothing Then If Target.Value <> "" Then ActiveSheet.Unprotect Password:="password" Target.Locked = True ActiveSheet.Protect Password:="password" End If ElseIf Not Intersect(Target, Range("P1")) Is Nothing Then If Target.Value = 1 Then Call SetRecipients End If End If LetsContinue: Application.EnableEvents = True Exit Sub justenditall: MsgBox Err.Description Resume LetsContinue End Sub Sub SetRecipients() MsgBox "Second One Runs" End Sub