如果没有阻止结束如果错误VBA

时间:2016-02-11 22:04:21

标签: vba excel-vba excel

我编写了这段代码,尝试根据使用vba< Rnd()函数和if语句生成的另一个变量的值为变量赋值,但由于某种原因,它给了我" end如果没有阻止,如果错误。"这只是代码的一部分,我为5个不同的产品迭代这个过程5次,并对聚合结果的数字生成器进行10000次迭代。最初我尝试嵌套所有东西,但是当它没有工作时,我尝试做单个if语句和相同的交易。对此有任何帮助都很棒。

Submit

5 个答案:

答案 0 :(得分:2)

If的语法允许两种变体:

  1. “内联”

    If {bool-expression} Then {do something}
    
  2. “块”

    If {bool-expression} Then
        {do something}
    End If
    
  3. 当您使用“内联”语法时,End If令牌是非法的。

    所以这个(即删除End If令牌)会让你的代码再次编译:

    If ProdE <= 0.1 Then DaysLateE = 2
    If 0.1 < ProdE <= 0.2 Then DaysLateE = 3
    If 0.2 < ProdE <= 0.3 Then DaysLateE = 4
    If 0.3 < ProdE <= 0.4 Then DaysLateE = 5
    If 0.4 < ProdE <= 0.5 Then DaysLateE = 6
    If 0.5 < ProdE <= 0.6 Then DaysLateE = 7
    If 0.6 < ProdE <= 0.7 Then DaysLateE = 8
    If 0.7 < ProdE <= 0.8 Then DaysLateE = 9
    If 0.8 < ProdE <= 0.9 Then DaysLateE = 10
    If 0.9 < ProdE <= 1 Then DaysLateE = 11
    

    但是,作为@Rohan K suggested,更好的选择是使用Select Case构造,因为现在,所有这些条件一直在评估 - 使用{ {1}}阻止,执行将在找到匹配条件后退出Select Case块,并作为奖励获得可读性:

    Select

    那么,当Select Case ProdE Case Is <= 0.1 DaysLateE = 2 Case Is <= 0.2 DaysLateE = 3 Case Is <= 0.3 DaysLateE = 4 Case Is <= 0.4 DaysLateE = 5 Case Is <= 0.5 DaysLateE = 6 Case Is <= 0.6 DaysLateE = 7 Case Is <= 0.7 DaysLateE = 8 Case Is <= 0.8 DaysLateE = 9 Case Is <= 0.9 DaysLateE = 10 Case Is <= 1 DaysLateE = 11 Case Else 'DaysLateE = ?? End Select 大于或等于1时会发生什么? (还没有读到ProdE来自哪里,没关系)似乎ProdEProdE之间存在直线关系 - 你可以试着想出一个公式来计算它。

    这可能并不完美,但非常接近:

    DaysLateE

    然后您不需要DaysLateE = Int(ProdE * 10 - 0.000000000001) + 2 If块。

答案 1 :(得分:1)

问题是你最后的If声明。在没有If的情况下在一行上都有End If语句是完全有效的语法。因此,当您将End If放在那里时,它会在If语句后面加上一行。

这两个没有错误有效

If TotalDaysLate > 15 then CountE = CountE + 1

或者

If TotalDaysLate > 15 Then 
    CountE = CountE + 1
End If

答案 2 :(得分:1)

试试这个:我建议像这样使用Select Case这个

    For i = 0 To 10000

ProdE = Rnd()
ProdF = Rnd()
ProdG = Rnd()
ProdH = Rnd()
ProdI = Rnd()

If ProdE <= 0.1 Then
DaysLateE = 2
End If

If 0.1 < ProdE And ProdE <= 0.2 Then
DaysLateE = 3
End If

If 0.2 < ProdE And ProdE <= 0.3 Then
DaysLateE = 4
End If

If 0.3 < ProdE And ProdE <= 0.4 Then
DaysLateE = 5
End If

If 0.4 < ProdE And ProdE <= 0.5 Then
DaysLateE = 6
End If

If 0.5 < ProdE And ProdE <= 0.6 Then
DaysLateE = 7
End If

If 0.6 < ProdE And ProdE <= 0.7 Then
DaysLateE = 8
End If

If 0.7 < ProdE And ProdE <= 0.8 Then
DaysLateE = 9
End If

If 0.8 < ProdE And ProdE <= 0.9 Then
DaysLateE = 10
End If

If 0.9 < ProdE And ProdE <= 1 Then
DaysLateE = 11
End If


TotalDaysLateE = DaysLateE + 8
SumDaysLateE = SumDaysLateE + TotalDaysLateE

If TotalDaysLateE > 15 Then
CountE = CountE + 1
End If

答案 3 :(得分:0)

当您使用单行ifs时,您不需要编写End if。检查一下:https://msdn.microsoft.com/en-us/library/office/gg251599.aspx

答案 4 :(得分:0)

考虑到你的分支和条件路径,以及10000次迭代,我建议你只是将If... Then语句分成两部分。更好的是,将它与两个较小的Case... Select结合使用,可以轻松读取所有建议的组合。更快!

If ProdE <= 0.5 Then
    If ProdE <= 0.1 Then DaysLateE = 2
    If 0.1 < ProdE <= 0.2 Then DaysLateE = 3
    If 0.2 < ProdE <= 0.3 Then DaysLateE = 4
    If 0.3 < ProdE <= 0.4 Then DaysLateE = 5
    If 0.4 < ProdE <= 0.5 Then DaysLateE = 6
Else
    If 0.5 < ProdE <= 0.6 Then DaysLateE = 7
    If 0.6 < ProdE <= 0.7 Then DaysLateE = 8
    If 0.7 < ProdE <= 0.8 Then DaysLateE = 9
    If 0.8 < ProdE <= 0.9 Then DaysLateE = 10
    If 0.9 < ProdE <= 1 Then DaysLateE = 11
End If