Vba Select Case in Excel

The "Select Case" statement is another logical function of Excel used to perform conditional execution of code. VBA CASE SELECT function in Excel is very useful when having a lot of different choices and having to branch from them. The advantage of SELECT CASE over IF THEN is that it is much more easy to read and implement.

This "Select" part defines the variable to be checked and the "Case" part defines the conditions

Syntax

Select Case <expression>

Case <condition1>
do result1
Case <condition2>
do result2

End Select

The Select Case statement can check for any number of conditions

Example: The following example selects a string variable and perform conditional execution of code

  1. Sub SelectCase()
  2. Dim str As String
  3. str = "Hello"
  4. Select Case str
  5. Case "Hello"
  6. MsgBox "Condition is True"
  7. Case "World"
  8. MsgBox "Condition is False"
  9. End Select
  10. End Sub

The important thing is, the statement executes until a true condition is met and the remaining conditions are ignored.

excel vba select case

The Result is as shown below

excel vba select case

 

You can find similar Excel Questions and Answer hereunder

1) Is there any way I can see more region of my WorkSheet?

2) I have read that Merging cells should be avoided if possible. In Excel, how can I then make my Title appear in the center of my Data Table?

3) Here some explanations about xml and how to use xml in VBA

4) I have several pictures that are misaligned - manually aligning them is very laborious. Is there an automatic alignment option?

5) How can I get users to select a file for processing using my macro?

6) I have to enter non alphanumeric characters in a cell using VBA - how can I get their codes for use in VBA?

7) How to get user input in VBA with the inputbox function

8) I have 2 sets of lists from 2 different reports - how can I select the cells with differences?

9) Can I add a small chart to Table data to make it visually appealing and easy to interpret?

10) How to hide and unhide rows and columns in excel VBA

 

Here the previous and next chapter