【问题标题】:Else without If Compilation Error没有If编译错误的Else
【发布时间】:2014-08-10 13:10:37
【问题描述】:

我不确定为什么这会引发编译错误并告诉我我有一个没有 If 的 Else。错误出现在 ElseIf 行上。其他一切似乎都在工作。有什么建议?

If IsEmpty(Me.ComboBox15.Value) = False Then
    strFind = Me.ComboBox15.Value    'what to look for
    With Ws.Range("cGroup")  'where to look
    Sheet2.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=ComboBox15.Value
    Set c = .Find(strFind, LookIn:=xlValues)
        'c.Select
        If Not c Is Nothing Then
            With Me
                .ComboBox15.Value = c.Offset(0, 0).Value
                .ComboBox16.Value = c.Offset(0, 1).Value
                .ComboBox17.Value = c.Offset(0, 2).Value
                .ComboBox1.Value = c.Offset(0, 3).Value
                .ComboBox19.Value = c.Offset(0, 4).Value
                r = c.Row
                f = 0
            End With
            FirstAddress = c.Address
        Else: MsgBox strFind & "Not listed"    'search failed
        End If
ElseIf IsEmpty(Me.ComboBox16.Value) = False Then
    strFind = Me.ComboBox16.Value    'what to look for
        With Ws.Range("cGeography")  'where to look
        Sheet2.ListObjects("Table1").Range.AutoFilter Field:=2,  Criteria1:=ComboBox16.Value
        Set c = .Find(strFind, LookIn:=xlValues)
            'c.Select
            If Not c Is Nothing Then
                With Me
                    .ComboBox15.Value = c.Offset(0, -1).Value
                    .ComboBox16.Value = c.Offset(0, 0).Value
                    .ComboBox17.Value = c.Offset(0, 1).Value
                    .ComboBox1.Value = c.Offset(0, 2).Value
                    r = c.Row
                    f = 0
                End With
                FirstAddress = c.Address
            Else: MsgBox strFind & "Not listed"    'search failed
            End If
        End With
ElseIf Not IsEmpty(Me.ComboBox17.Value) Then
            strFind = Me.ComboBox17.Value    'what to look for
            With Ws.Range("cDept")  'where to look
            Sheet2.ListObjects("Table1").Range.AutoFilter Field:=3, Criteria1:=ComboBox17.Value
            Set c = .Find(strFind, LookIn:=xlValues)
                'c.Select
                If Not c Is Nothing Then
                    With Me
                        .ComboBox15.Value = c.Offset(0, -2).Value
                        .ComboBox16.Value = c.Offset(0, -1).Value
                        .ComboBox17.Value = c.Offset(0, 0).Value
                        .ComboBox1.Value = c.Offset(0, 1).Value
                        r = c.Row
                        f = 0
                    End With
                    FirstAddress = c.Address
                Else: MsgBox strFind & "Not listed"    'search failed
                End If
            End With


Else: MsgBox "Error"
End If

对于任何格式/缩进错误,我深表歉意。我是 VBA 的新手。我搜索了答案,但我找到的一切看起来都是这样的。

【问题讨论】:

    标签: excel if-statement compiler-errors excel-2007 vba


    【解决方案1】:

    这是因为With 块:

    With Ws.Range("cGroup")  'where to look
    

    永远不会在它所在的 If 块内结束。

    【讨论】:

    • 结束 If 应该关闭以 If Not c Is Nothing Then 开头的块(对不起,我不知道如何格式化)。我尝试删除它以防万一,但我仍然遇到同样的错误。 @Soren
    • 你是对的。不是这样。我已经更新了我的答案。
    • 做到了!非常感谢!!
    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 2023-04-05
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多