VBA:突破深度嵌套的 If 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19844311/
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-12 00:19:44  来源:igfitidea点击:

VBA: Break Out of Deeply Nested If Statements

vbaif-statementbreakcontrol-flownested-if

提问by user1205577

Typically when I want to break out of a statement I just set a booleanflag for control flow, but I have a special case with many nested Ifstatements and I'd really like to have a way to break out of several with one simple statement.

通常,当我想跳出一个语句时,我只是boolean为控制流设置一个标志,但是我有一个带有许多嵌套If语句的特殊情况,我真的很想有一种方法可以用一个简单的语句跳出多个语句。

In Java you can name a loopand then break to that location; is there anything like that for VBA that can be used from a deeply nested location in Ifstatements? I know VBA has the Exit statementfor loops (while, for, etc), so I'm wondering if there is something similar for Ifs.

在 Java 中,您可以命名一个循环,然后中断到该位置;是否有类似 VBA 的东西可以从If语句中的深层嵌套位置使用?我知道 VBA 有循环的Exit 语句whilefor等),所以我想知道Ifs是否有类似的东西。

Ideally I'd like to do something this:

理想情况下,我想做这样的事情:

If ...
    *NAMED_IF*
    If ...
         If ...
          :
            *break out of NAMED_IF*
          :
        End If
    End If
    *Now We end up at this control position*
End If

回答by Netloh

There isn't an if-statement specific method to break out of nested if-statements, but you could use the GoTo-statement instead:

没有 if 语句特定的方法来打破嵌套的 if 语句,但您可以使用GoTo-statement 代替:

If ...
    '*NAMED_IF*
    If ...
         If ...

            '*break out of NAMED_IF*'
            GoTo GoToHere

        End If
    End If
End If

'*Now We end up at this control position*
GoToHere: