if语句的编译器错误

时间:2017-10-16 12:49:49

标签: vba excel-vba excel

我是编码/脚本编程的新手。它是一个学校项目,我将不得不更改以下代码以将Application.EnableEvents添加到现有代码中以抑制其他宏中的Change事件。

我试图更改代码,但是如果没有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

End Sub

1 个答案:

答案 0 :(得分: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
相关问题