Forum Discussion

LemonPeeler's avatar
LemonPeeler
Copper Contributor
Aug 25, 2022

How do we exit a CASE SELECT block?

How do we exit an Excel SELECT CASE block? We have found a Microsoft Docs indicating that you can only use the EXIT SELECT command if it is between the SELECT (or SELECT CASE) and the matching END SELECT. Yet, whenever we try to use this, we get a compile error.

 

We are attempting to use the EXIT SELECT to end processing commands in one of the case choices if a certain condition is met. However, we don’t seem to have a way to exit prematurely to avoid executing additional commands in the selected case using this command.

  • JoeUser2004's avatar
    JoeUser2004
    Bronze Contributor

    LemonPeeler 

     

    Once again, you are asking a syntax question without showing us the complete context in one form or another.

     

    According to Excel VBA help, there is no Exit Select statement.  Use a GoTo instead.

     

    I think you are looking at documentation for .NET Visual Basic.  That is a stand-alone compiler.  Technically, it is a different language with different features and possibly different behaviors.

     

    Admittedly, I have fallen into the same trap myself, because the two languages are "similar enough" perhaps 90% of the time.

    • LemonPeeler's avatar
      LemonPeeler
      Copper Contributor
      Thank you for the response. As a novice, we probably missed that the article didn't apply to Excel VBA. Furthermore, we solved this problem in the end when we solved the issue of using the GOTO statement, which was not functioning for us due to another error, which was also resolved. Thank you again!
    • LemonPeeler's avatar
      LemonPeeler
      Copper Contributor
      Hello Joe, one other question if we may. Can we include a RESUME NEXT in a SELECT CASE routine to exit the select case, without processing subsequent commands, and then process the next item in a FOR loop?
      • JoeUser2004's avatar
        JoeUser2004
        Bronze Contributor

        LemonPeeler  wrote:  ``Can we include a RESUME NEXT in a SELECT CASE routine to exit the select case``

         

        No.  From the VBA help page for the Resume statement:  ``Resumes execution after an error-handling routine is finished.``  To learn more about error-handling (albeit not relevant to your questions), see the VBA help page for the On Error statement.

         

        For VBA help pages, I select offline help, not online help.  Then I enter a keyword into the search window to the right of Help on the VBA toolbar.

         

        -----

        LemonPeeler  wrote:  ``[...] and then process the next item in a FOR loop?``

         

        No.  And there is no Continue statement in VBA.

         

        Use GoTo for both purposes.

         

  • JoeUser2004's avatar
    JoeUser2004
    Bronze Contributor

    LemonPeeler 

     

    PS, I meant to add....  Are aware that there is an implied "exit select" at the end of each Case block?

     

    In other words, for example:

     

    Select Case expression

         Case expression

              statements

         Case expression

              statements

    End Select

     

    is equivalent to:

     

    Select Case expression

         Case expression

              statements

              GoTo afterEnd

         Case expression

              statements

              GoTo afterEnd

    End Select

    afterEnd:

         statements

     

     

    So no "exit" or explicit GoTo statement is necessary, if that is all you want.

    • LemonPeeler's avatar
      LemonPeeler
      Copper Contributor
      Again, thank you for taking the time to respond. As you suggest here, this method worked fine after we resolved the issue of our GOTO statement compile error. The compile error resulted after inserting a code line break (underscore) after a THEN statement and before the subsequent commands. Thanks.

Resources