【问题标题】:Error : Else without if错误:否则没有 if
【发布时间】:2017-03-01 16:14:54
【问题描述】:

我正在尝试建立一个支出与收入的 Excel 并得到这个恼人的错误 “否则没有如果” 我浏览了不同的论坛和其他但无法得到答案 - 这让我发疯了

有点解释:我正在查看我在工作表中名为“数据”的表格,如果我发现一个不为空白或没有“0”的单元格,我会继续。第一个“for each”是针对类别的,第二个是针对子类别的——一旦我找到一个子类别,我就会搜索我们在该子类别下的成本。我在月表中查找他的名字在字符串 CSheet 中的成本 - 当我找到它们时,我将它们添加到计数器 scSum 中。

With Worksheets("Data")
       For Each Mcell In Worksheets("Data").Range(.Cells(CatRow, stCatCol), .Cells(CatRow, enCatCol)).Cells '("C1:X1") expenses
           scSum = 0
          If Mcell.Value <> 0 Then
                   If IsEmpty(Mcell.Value) Then Exit For
                   Else
                            For Each eScell In Worksheets("Data").Range(.Cells(CatRow + 1, Mcell.Column), .Cells(CatRow + noSubCat, Mcell.Column)).Cells 'run on sub category
                           'eScell - expence category cell
                               If IsEmpty(eScell.Value) Then Exit For
                               Else ***'error!***
                               subC = eScell.Value
                                  With Worksheets(CSheet)
                                      For Each eCcell In Worksheets(CSheet).Range(.Cells(ExpMonthStRow, ExpMonthSubCol), .Cells(MonthEndRow, ExpMonthSubCol)).Cells 'run in month and look for sub category expences
                                         'eCcell - expence cost cell
                                          If IsEmpty(eCcell.Value) Then Exit For
                                          Else
                                              If eCcell.Value = subC Then
                                                  scSum = scSum + Worksheets(CSheet).Cells(eCcell.row, ExpMonthSubCostCol)
                                              End If
                                          End If
                                      Next eCcell
                                  End With
                               End If
                            Next eScell
                    End If
        'incomes
        scSum = 0
            End If
        Next Mcell
End With

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    在 VB 中THEN 之后必须换行,否则IF 将被隐式关闭。

    您可以在 SO 上与其他问题进行比较:

    VBA: Else without If Error

    【讨论】:

    • +1 澄清一下,您可能想将“隐含”(这听起来很粗鲁)改为“隐含”。
    【解决方案2】:

    查找违规End If 的最佳方法是Indent 您的代码。 :)

    IF-Endif可以用下面的方式写。

    多个语句

    If <Condition> Then 
        <Do Something>
    Else
        <Do Something Else>
    End If
    

    If <Condition1> Then
        <Do Something>
    ElseIf <Condition2> Then
        <Do Something Else>
    Else
        <Do Something Else>
    End If
    

    单一陈述

    If <Condition> Then 
        <Do Something>
    End If
    

    上面的语句也可以写成一行

    If <Condition> Then <Do Something>
    

    在这种情况下,您不需要End If

    你上面的代码(去掉其他演示代码后)可以写成如下图。我删除了额外的代码来说明我的意思。在下面的代码中,所有以* 开头的行都不是必需的。

    If Mcell.Value <> 0 Then
        If IsEmpty(Mcell.Value) Then Exit For
    Else
        If IsEmpty(eScell.Value) Then Exit For
        *Else
        If IsEmpty(eCcell.Value) Then Exit For
        *Else
        If eCcell.Value = subC Then
            scSum = scSum + Worksheets(CSheet).Cells(eCcell.Row, ExpMonthSubCostCol)
        End If
    
        *End If
        *End If
        *End If
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      相关资源
      最近更新 更多