{"id":328,"date":"2013-02-27T13:28:54","date_gmt":"2013-02-27T13:28:54","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=328"},"modified":"2020-04-23T10:53:15","modified_gmt":"2020-04-23T10:53:15","slug":"excel-vba-lesson-13-select-case","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/","title":{"rendered":"Excel VBA Lesson 13: Using Select Case"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-12-if-then-else\/\">&lt;&lt;Lesson 12&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-14-looping\/\">&lt;&lt;Lesson 14&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>In lesson 4, we have learned how to handle codes in Excel VBA that involve decision-making process through the use the conditional statement If&#8230;.Then&#8230;.Else. However, for multiple options or selections programs, the If&#8230;Then&#8230;Else structure could become too bulky and difficult to debug if problems arise. Fortunately, Excel VBA provides another way to handle complex multiple-choice cases, that is, the Select Case&#8230;..End Select decision structure. The syntax of a Select Case&#8230;End Select structure is as follows:<\/p>\n\n\n\n<p>Select Case variable<br>\nCase value 1<br>\nStatement<br>\nCase value 2<br>\nStatement<br>\n.<br>\n.<br>\nCase Else<br>\nEnd Select<br>\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>\nWe shall demonstrate the use of Select Case&#8230;End Select in the following examples:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 13.1<\/strong><\/h4>\n\n\n\n<p>In this program, we want to automatically assign a remark in relation to a certain examination grade. For example, if the grade is A, we shall assign remark as High distinction, A- for distinction and so forth. To enter the code, start MS Excel then place a button and a label onto the spreadsheet. The label is for displaying the remark. Cells(1,1) is for the user to enter the grade.<\/p>\n\n\n\n<p>The code:<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<br>\nDim grade As String<br>\ngrade = Cells(1, 1)<br>\nSelect Case grade<\/p>\n\n\n\n<p>Case &#8220;A&#8221;<br>\nlabel1.Caption = &#8220;High Distinction&#8221;<\/p>\n\n\n\n<p>Case &#8220;A-&#8220;<br>\nlabel1.Caption = &#8220;Distinction&#8221;<\/p>\n\n\n\n<p>Case &#8220;B&#8221;<br>\nlabel1.Caption = &#8220;Credit&#8221;<\/p>\n\n\n\n<p>Case &#8220;C&#8221;<br>\nlabel1.Caption = &#8220;Pass&#8221;<\/p>\n\n\n\n<p>Case Else<br>\nlabel1.Caption = &#8220;Fail&#8221;<\/p>\n\n\n\n<p>End Select<br>\nEnd Sub<\/p>\n\n\n\n<p>The Output<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"355\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\" alt=\"Excel VBA\" class=\"wp-image-331\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Figure 13.1<\/strong><\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 13.2<\/strong><\/h4>\n\n\n\n<p>In this &nbsp;example, we shall show you how to process the grades of students according to the marks given. For example, if a student&#8217;s mark is 85, the corresponding grade is A and if the mark is 20 the grade will be F and so forth.<\/p>\n\n\n\n<p>The code<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<\/p>\n\n\n\n<p>Dim mark As Single<br>\nDim grade As String<br>\nmark = Cells(1, 1).Value<\/p>\n\n\n\n<p><span style=\"color: #008000;\">&#8216;To set the alignment to center<\/span><br>\nRange(&#8220;A1:B1&#8221;).Select<br>\nWith Selection<br>\n.HorizontalAlignment = xlCenter<br>\nEnd With<\/p>\n\n\n\n<p>Select Case mark<br>\nCase 0 To 20<br>\ngrade = &#8220;F&#8221;<br>\nCells(1, 2) = grade<br>\nCase 20 To 29<br>\ngrade = &#8220;E&#8221;<br>\nCells(1, 2) = grade<br>\nCase 30 To 39<br>\ngrade = &#8220;D&#8221;<br>\nCells(1, 2) = grade<br>\nCase 40 To 59<br>\ngrade = &#8220;C&#8221;<br>\nCells(1, 2) = grade<br>\nCase 60 To 79<br>\ngrade = &#8220;B&#8221;<br>\nCells(1, 2) = grade<br>\nCase 80 To 100<br>\ngrade = &#8220;A&#8221;<br>\nCells(1, 2) = grade<br>\nCase Else<br>\ngrade = &#8220;Error!&#8221;<br>\nCells(1, 2) = grade<br>\nEnd Select<\/p>\n\n\n\n<p>End Sub<\/p>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>To set the cell align alignment to center, we use the following procedure:<\/p>\n\n\n\n<p>Range(&#8220;A1:B1&#8221;).Select<br>\nWith Selection<br>\n.HorizontalAlignment = xlCenter<br>\nEnd With<\/p>\n\n\n\n<p>We can use the statement case&nbsp;<i>value1<\/i>&nbsp;to&nbsp;<i>value 2<\/i>&nbsp; to specify the range of values that fulfil the particular case.<\/p>\n\n\n\n<p>You should also include the error case where the values entered are out of the range or invalid. For example, if the examination mark is from 0 to 100, then any value out of this range is invalid. In this program, I use case else to handle the error entries.<\/p>\n\n\n\n<p>Figure 13.2 illustrates the output of this example.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_26_2008_vbaVI.gif\" alt=\"Excel VBA\"\/><\/figure>\n\n\n\n<p><strong>Figure 13.2<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-12-if-then-else\/\">&lt;&lt;Lesson 12&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-14-looping\/\">&lt;&lt;Lesson 14&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 12&gt;&gt; [Contents] &lt;&lt;Lesson 14&gt;&gt; In lesson 4, we have learned how to handle codes in Excel VBA that involve decision-making process through the use the conditional statement If&#8230;.Then&#8230;.Else. However, for multiple options or selections programs, the If&#8230;Then&#8230;Else structure could become too bulky and difficult to debug if problems arise. Fortunately, Excel VBA provides another &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 13: Using Select Case&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-328","page","type-page","status-publish","hentry","category-decison-making"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson13.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson13.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T10:53:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"304\" \/>\n\t<meta property=\"og:image:height\" content=\"355\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm\",\"name\":\"Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\",\"datePublished\":\"2013-02-27T13:28:54+00:00\",\"dateModified\":\"2020-04-23T10:53:15+00:00\",\"description\":\"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson13.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg\",\"width\":304,\"height\":355},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson13.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 13: Using Select Case\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/vba_lesson13.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems","og_url":"https:\/\/excelvbatutor.com\/vba_lesson13.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:53:15+00:00","og_image":[{"width":304,"height":355,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/","url":"https:\/\/excelvbatutor.com\/vba_lesson13.htm","name":"Excel VBA Lesson 13: Using Select Case - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg","datePublished":"2013-02-27T13:28:54+00:00","dateModified":"2020-04-23T10:53:15+00:00","description":"This Excel VBA lesson demonstrate the use of Select Case ....End Select in Excel VBA to handle multiple choices problems","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson13.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson13.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson13.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_example7.1.jpg","width":304,"height":355},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson13.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 13: Using Select Case"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=328"}],"version-history":[{"count":18,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/328\/revisions"}],"predecessor-version":[{"id":3458,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/328\/revisions\/3458"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}