2017-10-16 162 views
0

我是編程/腳本編程新手。它是一個學校項目,我將不得不更改下面的代碼,將Application.EnableEvents添加到現有代碼中,以抑制其他宏中的更改事件。if語句編譯錯誤

我試圖改變代碼,但我得到一個編譯錯誤,否則沒有如果。我驗證了語法,它看起來不錯。我在這裏做錯了什麼?我對「IF」陳述的理解不正確嗎?

Private Sub Worksheet_Change(ByVal Target As Range) 
    Application.EnableEvents = False 
    If Not Intersect(Target, Range("E43")) Is Nothing Then 
     With Range("E44") 
      If Target.Value = "Specific number of Days" Then 
       .Locked = False 
       .Activate 
      Else 
       'This handles **ANY** other value in the dropdown 
       .Locked = True 
       '.Clear 
      End If 
     End With 
     ElseIf Not Intersect(Target, Range("E30")) Is Nothing Then 
      If Target.Value = "YES" Then Call Class8 Else Call Class8User 
     ElseIf Not Intersect(Target, Range("E31")) Is Nothing Then 
      If Target.Value = "YES" Then Call Class7 Else Call Class7User 
    End If 
    Application.EnableEvents = True 
End Sub 

我想改變代碼如下。

Private Sub Worksheet_Change(ByVal Target As Range) 
Application.EnableEvents = False 
If Not Intersect(Target, Range("E43")) Is Nothing Then 
    With Range("E44") 
     If Target.Value = "Specific number of Days" Then 
      .Locked = False 
      .Activate 
     Else 
      'This handles **ANY** other value in the dropdown 
      .Locked = True 
      '.Clear 
     End If 
    End With 
    ElseIf Not Intersect(Target, Range("E30")) Is Nothing Then 
     If Target.Value = "YES" Then 
     Application.EnableEvents = False 
     Call Notify 
     Application.EnableEvents = True 
     Else 
     Application.EnableEvents = False 
     Call NotifyUser 
     Application.EnableEvents = True 
    ElseIf Not Intersect(Target, Range("E31")) Is Nothing Then 
     If Target.Value = "YES" Then 
     Application.EnableEvents = False 
     Call Delta 
     Application.EnableEvents = True 
     Else 
     Application.EnableEvents = False 
     Call DeltaUser 
     Application.EnableEvents = True 
     End If 
End If 
Application.EnableEvents = True 

末次

+2

你缺少'後'Application.EnableEvents =結束If'在每個ElseIf塊中都是真的。如果您遵循標準代碼縮進實踐,您會看到這種情況。 –

回答

1

始終縮進所有的代碼 - 那麼你可以很容易地看到你缺少end if

Private Sub x(ByVal Target As Range) 

    Application.EnableEvents = False 
    If Not Intersect(Target, Range("E43")) Is Nothing Then 
     With Range("E44") 
      If Target.Value = "Specific number of Days" Then 
       .Locked = False 
       .Activate 
      Else 
       'This handles **ANY** other value in the dropdown 
       .Locked = True 
       '.Clear 
      End If 
     End With 
    ElseIf Not Intersect(Target, Range("E30")) Is Nothing Then 
     If Target.Value = "YES" Then 
      Application.EnableEvents = False 
      Call notify 
      Application.EnableEvents = True 
     Else 
      Application.EnableEvents = False 
      Call notifyuser 
      Application.EnableEvents = True 
     End If  ' <-- This was missing 
    ElseIf Not Intersect(Target, Range("E31")) Is Nothing Then 
     If Target.Value = "YES" Then 
      Application.EnableEvents = False 
      Call delta 
      Application.EnableEvents = True 
     Else 
      Application.EnableEvents = False 
      Call deltaUser 
      Application.EnableEvents = True 
     End If  ' <-- This was missing 
    End If 
    Application.EnableEvents = True 

End Sub