2017-10-16 168 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