{"id":1199,"date":"2013-12-10T03:58:06","date_gmt":"2013-12-10T03:58:06","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=1199"},"modified":"2019-04-19T07:39:43","modified_gmt":"2019-04-19T07:39:43","slug":"excel-vba-2010-lesson-10-decision-making-1-if-then-else","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/","title":{"rendered":"Excel 2010 VBA Lesson 10: Using If&#8230;Then&#8230;Else"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-9-string-manipulation-functions\/\">[Lesson 9]<\/a>&lt;&lt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-tutorial\/\">[Table of Contents]<\/a>&gt;&gt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-11-decision-making-2-select-case-end-select\/\">[Lesson 11]<\/a><\/strong><\/h4>\n\n\n\n<p>Decision-making process is an important part of Excel 2010 VBA &nbsp;programming because it can help to solve problems that require the fulfillment of certain conditions.<\/p>\n\n\n\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><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><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script>\n\n\n\n<p>In Excel 2010 VBA, decision making involves the use of the<strong> If&#8230;then&#8230;Else<\/strong> syntax to process data and display the output based on the fulfillment of certain conditions. To effectively control the VB program flow, we need to use If&#8230;Then&#8230;Else statement together with the conditional operators and logical operators.<br><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><br><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\"><br><script>&lt;br \/>     (adsbygoogle = window.adsbygoogle || []).push({});&lt;br \/><\/script><br>These operators are shown in Table 10.1 and Table 10.2 respectively.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Table 10.1: Conditional Operators<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Table 10.2: Logical Operators<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>We shall demonstrate the usage of If\u2026.Then\u2026Else with the following example.In this program, we place the command button1 on the MS Excel spreadsheet and go into the Excel 2010 VBA editor by clicking on the button. In the editor, enter the program code as shown below.<\/p>\n\n\n\n<p>We use the RND function to generate random numbers. In order to generate random integers between 0 and 100, we combined the syntax Int(Rnd*100). For example, when Rnd=0.6543, then Rnd*100=65.43, and Int(65.43)=65. Using the statement cells(1,1).Value=mark will place the value of 65 into cell(1,1).<\/p>\n\n\n\n<p>Now, based on the mark in cells(1,1), we use the If\u2026\u2026.Then\u2026.Elseif statements to put the corresponding grade in cells(2,1). So, when you click on command button 1, it will insert a random number between 1 and 100 in cells(1,1) and the corresponding grade in cells(2,1).<\/p>\n\n\n\n<p><strong>The Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click()\nDim mark As Integer\nDim grade As String\n mark = Int(Rnd * 100)\n Cells(1, 1).Value = mark\nIf mark &lt; 20 And mark &gt;= 0 Then\n grade = \u201cF\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt; 30 And mark &gt;= 20 Then\n grade = \u201cE\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt; 40 And mark &gt;= 30 Then\n grade = \u201cD\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt; 50 And mark &gt;= 40 Then\n grade = \u201cC-\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt; 60 And mark &gt;= 50 Then\n grade = \u201cC\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt; 70 And mark &gt;= 60 Then\n grade = \u201cC+\u201d \n Cells(2, 1).Value = grade\nElseIf mark &lt; 80 And mark &gt;= 70 Then\n grade = \u201cB\u201d\n Cells(2, 1).Value = grade\nElseIf mark &lt;= 100 And mark &gt;=80 Then\n grade = \u201cA\u201d\n Cells(2, 1).Value = grade\nEnd If\nEnd Sub\n<\/pre>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><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><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<p>The output is shown in Figure 10.1<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig10.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"432\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_fig10.1.jpg\" alt=\"vba2010_fig10.1\" class=\"wp-image-1204\"\/><\/a><\/figure><\/div>\n\n\n\n<p><br><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><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><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-9-string-manipulation-functions\/\">[Lesson 9]<\/a>&lt;&lt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-tutorial\/\">[Table of Contents]<\/a>&gt;&gt;<a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-11-decision-making-2-select-case-end-select\/\">[Lesson 11]<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 9]&lt;&lt;[Table of Contents]&gt;&gt;[Lesson 11] Decision-making process is an important part of Excel 2010 VBA &nbsp;programming because it can help to solve problems that require the fulfillment of certain conditions. In Excel 2010 VBA, decision making involves the use of the If&#8230;then&#8230;Else syntax to process data and display the output based on the fulfillment of &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel 2010 VBA Lesson 10: Using If&#8230;Then&#8230;Else&#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-1199","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 2010 VBA Lesson 10: Decision Making using If...Then...Else<\/title>\n<meta name=\"description\" content=\"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA\" \/>\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\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel 2010 VBA Lesson 10: Decision Making using If...Then...Else\" \/>\n<meta property=\"og:description\" content=\"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/\" \/>\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=\"2019-04-19T07:39:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\" \/>\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-2010-lesson-10-decision-making-1-if-then-else\/\",\"url\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/\",\"name\":\"Excel 2010 VBA Lesson 10: Decision Making using If...Then...Else\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\",\"datePublished\":\"2013-12-10T03:58:06+00:00\",\"dateModified\":\"2019-04-19T07:39:43+00:00\",\"description\":\"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\",\"width\":323,\"height\":162},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel 2010 VBA Lesson 10: Using If&#8230;Then&#8230;Else\"}]},{\"@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 2010 VBA Lesson 10: Decision Making using If...Then...Else","description":"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA","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\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/","og_locale":"en_US","og_type":"article","og_title":"Excel 2010 VBA Lesson 10: Decision Making using If...Then...Else","og_description":"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA","og_url":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2019-04-19T07:39:43+00:00","og_image":[{"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/","url":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/","name":"Excel 2010 VBA Lesson 10: Decision Making using If...Then...Else","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg","datePublished":"2013-12-10T03:58:06+00:00","dateModified":"2019-04-19T07:39:43+00:00","description":"This lesson illustrates the usage of if...then...else statement in Excel 2010 VBA","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg","width":323,"height":162},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-2010-lesson-10-decision-making-1-if-then-else\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel 2010 VBA Lesson 10: Using If&#8230;Then&#8230;Else"}]},{"@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\/1199","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=1199"}],"version-history":[{"count":45,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/1199\/revisions"}],"predecessor-version":[{"id":3321,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/1199\/revisions\/3321"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}