2017-10-16 1 views
0

Je suis novice en codage/script. C'est un projet d'école, je devrais changer le code ci-dessous pour ajouter Application.EnableEvents au code existant pour supprimer l'événement Change dans d'autres macros.Erreur de compilation sur l'instruction if

J'ai essayé de changer le code, mais je reçois une erreur de compilation sans if. J'ai validé la syntaxe, ça a l'air OK. Qu'est-ce que je fais mal ici? Ma compréhension des énoncés «IF» est-elle incorrecte?

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 

J'essaie de changer le code comme ci-dessous.

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 

End Sub

+2

Il vous manque 'mettre fin If' après la' Application.EnableEvents = True' dans chacun des blocs 'ElseIf'. Vous verriez cela visuellement si vous suiviez les pratiques d'indentation de code standard. –

Répondre

1

toujours en retrait tout votre code - alors vous pouvez facilement voir où vous manquez le 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