繁体   English   中英

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

[英]Running multiple 'Private Sub Worksheet_Change(ByVal Target As Range)' in one worksheet

我试图将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 并且您缺少End If 我很惊讶代码完全在执行第一个IF / ENDIF

这对我有用 (经过反复测试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM