1. Single Expression or value as a Case Condition<\/strong><\/font>
\nIn the below example user will be asked to Enter a Number of the Day like 1, 2, 3…7 and using Select Case statement function will return the corresponding Name of the Day. Note: Here first day of the week is considered as “Monday”.<\/p>\n\n\nSub Find_Day_1()\nDim DayNumber As Integer\nDayNumber = InputBox(\"Enter the Day Number\")\nSelect Case DayNumber\n Case 1\n MsgBox (\"Monday\")\n Case 2\n MsgBox (\"Tuesday\")\n Case 3\n MsgBox (\"Wednesday\")\n Case 4\n MsgBox (\"Thursday\")\n Case 5\n MsgBox (\"Friday\")\n Case 6\n MsgBox (\"Saturday\")\n Case 7\n MsgBox (\"Sunday\")\n Case Else\n MsgBox (\"Invalid Day Number\")\nEnd Select\nEnd Sub\n<\/code><\/pre>\n<\/p>\n
2. Using a comma to separate multiple expressions or ranges in each Case Clause.<\/strong> <\/font><\/p>\nMultiple expressions or ranges can be specified in each Case clause, by separating each expression with a comma It is same as logical operator OR<\/strong>. In the below example in each of the Case clause there are two expression: One is numeric and other one is string. It means if you Input : 1 then Also it will return “Monday” and If you enter “Mon” then also it will return “Monday”.<\/p>\n\nSub Find_Day_2()\nDim DayNumber As Variant\nDayNumber = InputBox(\"Enter the Day Number\")\nSelect Case DayNumber\n Case 1, \"Mon\"\n MsgBox (\"Monday\")\n Case 2, \"Tue\"\n MsgBox (\"Tuesday\")\n Case 3, \"Wed\"\n MsgBox (\"Wednesday\")\n Case 4, \"Thurs\"\n MsgBox (\"Thursday\")\n Case 5, \"Fri\"\n MsgBox (\"Friday\")\n Case 6, \"Sat\"\n MsgBox (\"Saturday\")\n Case 7, \"Sun\"\n MsgBox (\"Sunday\")\n Case Else\n MsgBox (\"Invalid Day Number\")\nEnd Select\nEnd Sub\n<\/code><\/pre>\n<\/p>\n
3. Using To Keyword to Specify upper and lower limit of a range.<\/strong><\/font>
\nIf you want to compare a value within a range then you can use To<\/strong> Keyword in your case clause as shown in the below example. Below example will take Marks in Percentage as input and it will return the Grade of the Student based on which range does that % belongs to.<\/p>\n\nSub Find_Grade()\nDim Percentage As Integer\nPercentage = InputBox(\"Enter the Obtained Percentage Marks by a Student\")\nSelect Case Percentage\n Case 0 To 33\n MsgBox (\"Failed\")\n Case 33 To 44\n MsgBox (\"Third Div\")\n Case 45 To 59\n MsgBox (\"Second Div\")\n Case 60 To 74\n MsgBox (\"First Div\")\n Case 75 To 100\n MsgBox (\"First Div with Distinction\")\n Case Else\n MsgBox (\"Marks Obtained in % Can not be more than 100\")\nEnd Select\nEnd Sub\n<\/code><\/pre>\n<\/p>\n
4. Using the Is Keyword with a comparison operator to compare.<\/strong><\/font>
\nUsing Is<\/strong> keyword you can compare the case value using comparison operator like < , > ,=<, >= etc. as shown in the below example:<\/p>\n\nSub Find_Temp_Status()\nDim temp As Single\ntemp = InputBox(\"Enter the Temperature\")\nSelect Case temp\n Case Is >= 40\n MsgBox \"Extremely Hot\"\n Case Is >= 25\n MsgBox \"Moderately Hot\"\n Case Is >= 0\n MsgBox \"Cool Weather\"\n Case Is < 0\n MsgBox \"Extremely Cold\"\nEnd Select\nEnd Sub\n<\/code><\/pre>\n<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"Hello friends in the last article of VBA Programming Tutorial you have learnt the use of If..else statement. 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 structure. Syntax: Select Case […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[5204],"tags":[],"class_list":["post-12160","post","type-post","status-publish","format-standard","hentry","category-vba-programming"],"yoast_head":"\n
VBA Programming : Decision - Select Case - Let's excel in Excel<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n