Question

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

In Java you can name a loop and then break to that location; is there anything like that for VBA that can be used from a deeply nested location in If statements? I know VBA has the Exit statement for loops (while, for, etc), so I'm wondering if there is something similar for 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
Was it helpful?

Solution

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

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:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top