Select Case – VBA

Select-Case statement, also called as Switch Case in some other languages, checks a variable for different cases or values. If anyone of the case becomes true then the respective case is executed and the program ignores all other cases.

For example when you have call to a customer care, in the very beginning you have some recorded messages like “for billing support press 1, for technical support press 2, to go back to main menu press 9 and so on…” how this concept work ?

it is just an example of Select Case or Switch case where each key is bind with some selective operations.

Syntax

Select Case test_expression

Case condition_1

result_1

 

Case condition_2

result_2

Case condition_n

result_n

Case Else

result_else

End Select

Example:

Consider that we have the score obtain by engineering student in his academic exam stored in Cell A1. Now on the base of that score we have to print status as given-

Sub MyChoice()

Dim score As Integer

score = Range(“A1”).Value

Select Case score

Case Is >= 80

result = “very good”

Case Is >= 70

result = “good”

Case Is >= 605

result = “average”

Case Else

result = “poor”

End Select

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/04/05/select-case-vba
YouTube39
YouTube