{"id":12160,"date":"2012-10-13T19:18:44","date_gmt":"2012-10-13T19:18:44","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=2406"},"modified":"2022-08-12T11:15:12","modified_gmt":"2022-08-12T11:15:12","slug":"select-case-statement","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/10\/select-case-statement\/","title":{"rendered":"VBA Programming : Decision – Select Case"},"content":{"rendered":"
Hello friends in the last article<\/a> of VBA Programming Tutorial<\/a> you have learnt the use of If..else statement<\/a>. In this lesson you are going to learn another decision making statements in VBA. It means it is another way to control the flow of program, that is nothing but Select… Case<\/strong> structure. Case<\/strong> <condition_1> Case Else<\/strong> End Select<\/strong> Test Expression:<\/strong> is a string or numeric value which you are going to compare with the list of conditions. Case Else<\/strong> Statement gets executed when none of the conditions are true for a Test Expression. Note that this Statement is an Optional Statement. It means it is not necessary to put Case Else Statement unless you want to execute a piece of code to be executed even when none of the conditions are matching. Select Case Statement<\/p><\/div> It is always a good practice to use Select Case Statement<\/strong> when multiple If-Else conditions are involved. As the number of If-Else conditions increases, debugging and understanding all the flow becomes tedious job. To overcome that problem, It is advised to use Select Case statement in nested If-Else conditions.<\/p>\n
\n <\/p>\nSyntax:<\/h2>\n
\n Select Case<\/strong> <test_expression><\/p>\n
\n <Your code 1>
\n Case<\/strong> <condition_2>
\n <Your code 2>
\n …
\n Case<\/strong> <condition_n>
\n <your code n><\/p>\n
\n <your else code><\/p>\n
\n<\/font><\/p>\nWhere:<\/h3>\n
\nCondition 1, 2…n:<\/strong> These are the different conditions. Whichever condition is going to match with the value of Test Expression, Statements under that condition will be executed. Rest other codes in other conditions will be ignored.
\nYour Code 1, 2…n:<\/strong> is the code which is going to be execute when the condition is found true.
\n <\/p>\nNote:<\/h3>\n
\n
\n
\n<\/p>\nUse of Case Statement<\/h2>\n
Example:<\/h2>\n