What's the purpose of `Case` with VBA's `Select` statement?

VBA has a Select statement that chooses which code to run based on a value (much like switch in C-style languages). The syntax for this statement looks like:

Select Case x
    Case 1
        'do one thing
    Case 2
        'do a different thing
End Select

What is the purpose of the Case right after Select? Alternatively, what is the meaning of Select without Case?


There is no such thing as a Select statement in VBA. There is a Select Case statement.

Therefore, strictly speaking, the answers to your questions are: There is none, and there is none.

It's a bit like asking what is the meaning of Pri without nt and vice-versa... There is no particularly exciting answer to that question.

It is simply a syntactical requirement of the Select Case statement in VBA: Select Case Statement

Select without Case will cause a compile error.

Without the various Case statements, the VBA compiler would not be able to differentiate between the code that you would want executed for each situation.

Imagine your code without the case statements:

Select Case x

     'do one thing

     'do a different thing
End Select

How do you know when to do which thing?

You can do the same thing as the Select Case statement with If statements

If x Then
     'do one thing
ElseIf x Then
     'do a different thing
End If

What if we got rid of the If statements? It wouldn't work the way you wanted it to.

Like others have said, the Case statement is just a necessary part of VBA.

Here's link that provides a good overview of the Select Case Statement along with some examples: Select Case

Need Your Help

What is the fastest way to find an array within another array in Java?

java arrays search

Is there any equivalent of String.indexOf() for arrays? If not, is there any faster way to find an array within another other than a linear search?

Adding object to DataTable and create a dynamic GridView

c# asp.net jquery

I have a lot of this working and have created a dropdown and a treeview using this technique. An ajax call to create controls on the fly and return them to the page fully constructed and configured...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.