簡體   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