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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7736796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:14:25  来源:igfitidea点击:

Block if without end if - VBA Error

ms-accessvbaif-statement

提问by Soham

This is a small snippet of the code I am writing. It seems to throw an "BLOCK IF WITHOUT END IF" error on the last line. I can't spot an error here, after consulted with this MSDNlink

这是我正在编写的代码的一小段。它似乎在最后一行抛出“BLOCK IF WITHOUT END IF”错误。在咨询了此MSDN链接后,我无法在此处发现错误

    If Longs > 10 & Shorts > 10 Then
                 If Longs < Shorts Then
                       Pairs = Longs
                 Else
                       Pairs = Shorts
                 End If
    Else
      If Longs < 10 & Shorts > 10 Then
                      Shortfall = True
                      Pairs = 10
     Else: Shortfall = False
                      Pairs = 10 
End If              
End Sub

Any help would be highly appreciated

任何帮助将不胜感激

采纳答案by Hans Olsson

You've got 3 Ifbut only 2 End If, change the End Subat the end to End If.

您有 3If但只有 2 End IfEnd Sub将末尾的更改为End If

If you indent the code properly, it'll be much easier to see:

如果您正确缩进代码,就会更容易看到:

If Longs > 10 & Shorts > 10 Then
   If Longs < Shorts Then
      Pairs = Longs
   Else
      Pairs = Shorts
   End If
Else
   If Longs < 10 & Shorts > 10 Then
      Shortfall = True
      Pairs = 10
   Else 
      Shortfall = False
      Pairs = 10 
   End If              
' missing End If
End Sub

回答by Jean-Fran?ois Corbett

Change your

改变你的

Else
    If Longs < 10 & Shorts > 10 Then

to

ElseIf Longs < 10 & Shorts > 10 Then

That way you're not unnecessarily nesting Ifconstructs, but just adding an extra condition to your outer Ifconstruct.

这样你就不会不必要地嵌套If构造,而只是向外部If构造添加额外的条件。

Like this:

像这样:

If Longs > 10 & Shorts > 10 Then
    If Longs < Shorts Then
        Pairs = Longs
    Else
        Pairs = Shorts
    End If
ElseIf Longs < 10 & Shorts > 10 Then
    Shortfall = True
    Pairs = 10
Else 
    Shortfall = False
    Pairs = 10 
End If              

Heck, you can even leave this intact, even though it's quite ugly:

哎呀,您甚至可以保持原样,即使它很丑陋:

Else: Shortfall = False
                Pairs = 10 

回答by Patrick Honorez

Change
Else: Shortfall = False
into

更改
Else: Shortfall = False

Else 
   Shortfall = False
   Pairs = 10
end if