{"id":1672,"date":"2020-07-09T14:13:20","date_gmt":"2020-07-09T21:13:20","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=1672"},"modified":"2020-07-11T11:04:47","modified_gmt":"2020-07-11T18:04:47","slug":"how-to-use-excel-vba-select-case","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=1672","title":{"rendered":"How to use Excel VBA &#8220;Select Case&#8221;"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"select-case-statement\">Select Case statement<\/h3>\n\n\n\n<p>Executes one of several groups of&nbsp;statements, depending on the value of an&nbsp;expression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"syntax\">Syntax<\/h3>\n\n\n\n<p><strong>Select Case<\/strong>&nbsp;<em>testexpression<\/em><br>[&nbsp;<strong>Case<\/strong>&nbsp;<em>expressionlist-n<\/em>&nbsp;[&nbsp;<em>statements-n<\/em>&nbsp;]]<br>[&nbsp;<strong>Case Else<\/strong>&nbsp;[&nbsp;<em>elsestatements<\/em>&nbsp;]]<br><strong>End Select<\/strong><\/p>\n\n\n\n<p>The&nbsp;<strong>Select Case<\/strong>&nbsp;statement syntax has these parts:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Part<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><em>testexpression<\/em><\/td><td>Required. Any&nbsp;numeric expression&nbsp;or&nbsp;string expression.<\/td><\/tr><tr><td><em>expressionlist-n<\/em><\/td><td>Required if a&nbsp;Case&nbsp;appears.<br><br>Delimited list of one or more of the following forms:&nbsp;expression,&nbsp;expression to expression,&nbsp;<strong>Is<\/strong><em> comparison operator<\/em>&nbsp;<em>expression<\/em>.<br><br>The&nbsp;<strong>To<\/strong>&nbsp;keyword&nbsp;specifies a range of values. If you use the&nbsp;<strong>To<\/strong>&nbsp;keyword, the smaller value must appear before&nbsp;<strong>To<\/strong>.<br><br>Use the&nbsp;<strong>Is<\/strong>&nbsp;keyword with&nbsp;comparison operators&nbsp;(except&nbsp;<strong>Is<\/strong>&nbsp;and&nbsp;<strong>Like<\/strong>) to specify a range of values. If not supplied, the&nbsp;<strong>Is<\/strong>&nbsp;keyword is automatically inserted.<\/td><\/tr><tr><td><em>statements-n<\/em><\/td><td>Optional. One or more statements executed if&nbsp;<em>testexpression<\/em>&nbsp;matches any part of&nbsp;<em>expressionlist-n<\/em>.<\/td><\/tr><tr><td><em>elsestatements<\/em><\/td><td>Optional. One or more statements executed if&nbsp;<em>testexpression<\/em>&nbsp;doesn&#8217;t match any of the&nbsp;<strong>Case<\/strong>&nbsp;clause.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"remarks\">Remarks<\/h3>\n\n\n\n<p>If&nbsp;<em>testexpression<\/em>&nbsp;matches any&nbsp;<strong>Case<\/strong>&nbsp;<em>expressionlist<\/em>&nbsp;expression, the&nbsp;<em>statements<\/em>&nbsp;following that&nbsp;<strong>Case<\/strong>&nbsp;clause are executed up to the next&nbsp;<strong>Case<\/strong>&nbsp;clause, or, for the last clause, up to&nbsp;<strong>End Select<\/strong>. Control then passes to the statement following&nbsp;<strong>End Select<\/strong>. If&nbsp;<em>testexpression<\/em>&nbsp;matches an&nbsp;<em>expressionlist<\/em>&nbsp;expression in more than one&nbsp;<strong>Case<\/strong>&nbsp;clause, only the statements following the first match are executed.<\/p>\n\n\n\n<p>The&nbsp;<strong>Case Else<\/strong>&nbsp;clause is used to indicate the&nbsp;<em>elsestatements<\/em>&nbsp;to be executed if no match is found between the&nbsp;<em>testexpression<\/em>&nbsp;and an&nbsp;<em>expressionlist<\/em>&nbsp;in any of the other&nbsp;<strong>Case<\/strong>&nbsp;selections. Although not required, it is a good idea to have a&nbsp;<strong>Case Else<\/strong>&nbsp;statement in your&nbsp;<strong>Select Case<\/strong>&nbsp;block to handle unforeseen&nbsp;<em>testexpression<\/em>&nbsp;values. If no&nbsp;<strong>Case<\/strong>&nbsp;<em>expressionlist<\/em>&nbsp;matches&nbsp;<em>testexpression<\/em>&nbsp;and there is no&nbsp;<strong>Case Else<\/strong>&nbsp;statement, execution continues at the statement following&nbsp;<strong>End Select<\/strong>.<\/p>\n\n\n\n<p>You can use multiple expressions or ranges in each&nbsp;<strong>Case<\/strong>&nbsp;clause. For example, the following line is valid:VBCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Case 1 To 4, 7 To 9, 11, 13, Is > MaxNumber <\/code><\/pre>\n\n\n\n<p>&nbsp;Note<\/p>\n\n\n\n<p>The&nbsp;<strong>Is<\/strong>&nbsp;comparison operator is not the same as the&nbsp;<strong>Is<\/strong>&nbsp;keyword used in the&nbsp;<strong>Select Case<\/strong>&nbsp;statement.<\/p>\n\n\n\n<p>You also can specify ranges and multiple expressions for character strings. In the following example,&nbsp;<strong>Case<\/strong>&nbsp;matches strings that are exactly equal to&nbsp;<code>everything<\/code>, strings that fall between&nbsp;<code>nuts<\/code>&nbsp;and&nbsp;<code>soup<\/code>&nbsp;in alphabetic order, and the current value of&nbsp;<code>TestItem<\/code>:VBCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Case \"everything\", \"nuts\" To \"soup\", TestItem <\/code><\/pre>\n\n\n\n<p><strong>Select Case<\/strong>&nbsp;statements can be nested. Each nested&nbsp;<strong>Select Case<\/strong>&nbsp;statement must have a matching&nbsp;<strong>End Select<\/strong>&nbsp;statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example<\/h3>\n\n\n\n<p>This example uses the&nbsp;<strong>Select Case<\/strong>&nbsp;statement to evaluate the value of a variable. The second&nbsp;<strong>Case<\/strong>&nbsp;clause contains the value of the variable being evaluated, and therefore only the statement associated with it is executed.VBCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim Number \nNumber = 8    ' Initialize variable. \nSelect Case Number    ' Evaluate Number. \nCase 1 To 5    ' Number between 1 and 5, inclusive. \n    Debug.Print \"Between 1 and 5\" \n' The following is the only Case clause that evaluates to True. \nCase 6, 7, 8    ' Number between 6 and 8. \n    Debug.Print \"Between 6 and 8\" \nCase 9 To 10    ' Number is 9 or 10. \nDebug.Print \"Greater than 8\" \nCase Else    ' Other values. \n    Debug.Print \"Not between 1 and 10\" \nEnd Select<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Select Case statement Executes one of several groups of&nbsp;statements, depending on the value of an&nbsp;expression. Syntax Select Case&nbsp;testexpression[&nbsp;Case&nbsp;expressionlist-n&nbsp;[&nbsp;statements-n&nbsp;]][&nbsp;Case Else&nbsp;[&nbsp;elsestatements&nbsp;]]End Select The&nbsp;Select Case&nbsp;statement syntax has these parts: Part Description testexpression Required. Any&nbsp;numeric expression&nbsp;or&nbsp;string expression. expressionlist-n Required if a&nbsp;Case&nbsp;appears. Delimited list of one or more of the following forms:&nbsp;expression,&nbsp;expression to expression,&nbsp;Is comparison operator&nbsp;expression. The&nbsp;To&nbsp;keyword&nbsp;specifies a range of <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=1672\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10,18],"tags":[220,221],"class_list":["post-1672","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-microsoft-office","tag-excel-vba","tag-select-case"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/1672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1672"}],"version-history":[{"count":0,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/1672\/revisions"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}