Aug 25 2022 12:04 PM
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.
Aug 25 2022 12:21 PM - edited Aug 25 2022 12:38 PM
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.
Aug 25 2022 12:27 PM - edited Aug 25 2022 12:28 PM
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.
Aug 25 2022 01:31 PM
Aug 25 2022 01:35 PM
Aug 25 2022 01:40 PM
Aug 25 2022 02:46 PM
@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.