{"id":288,"date":"2013-02-27T07:31:14","date_gmt":"2013-02-27T07:31:14","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=288"},"modified":"2020-04-23T10:52:48","modified_gmt":"2020-04-23T10:52:48","slug":"excel-vba-lesson-12-if-then-else","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-12-if-then-else\/","title":{"rendered":"Excel VBA Lesson 12: Using If&#8230;..Then&#8230;.Else"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-11-date-time-functions\/\">&lt;&lt;Lesson 11&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/\">&lt;&lt;Lesson 13&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>In this lesson, you will learn how to create an Excel VBA code that can make a decision. Decision-making process is an important part of Excel VBA &nbsp;programming because it can help to solve practical problems intelligently so that it can provide useful feedback to the users. In Excel VBA, decision making involves the use of If&#8230;Then&#8230;Else syntax to process data and display the output based on certain conditions. To effectively control the VB program flow, we shall use <b>If&#8230;Then&#8230;Else<\/b>&nbsp;statement together with the conditional operators and logical operators. These operators are shown in Table 12.1 and Table 12.2 respectively.<\/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><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"323\" height=\"162\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\" alt=\"vba_table4.1\" class=\"wp-image-293\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Table 12.1:Conditional Operators<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"132\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.2.jpg\" alt=\"vba_table4.2\" class=\"wp-image-294\"\/><\/a><\/figure><\/div>\n\n\n\n<p><strong>Table 12.2 Logical Operators<\/strong><\/p>\n\n\n\n<p>We shall demonstrate the usage of If&#8230;.Then&#8230;Else with the following example.In this program, we place the command button1 on the MS Excel spreadsheet and go into the VB editor by clicking on the button. At the VB editor, enter the program code as shown below. 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><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>\nNow, based on the mark in cells(1,1), I use the If&#8230;&#8230;.Then&#8230;.Elseif statements to put the corresponding grade in cells(2,1). So, when you click on command button 1, it will put 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<p>Private Sub CommandButton1_Click()<br>\nDim mark As Integer<br>\nDim grade As String<br>\nRandomize Timer<br>\nmark = Int(Rnd * 100)<br>\nCells(1, 1).Value = mark<br>\nIf mark &lt; 20 And mark &gt;= 0 Then<br>\ngrade = &#8220;F&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 30 And mark &gt;= 20 Then<br>\ngrade = &#8220;E&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 40 And mark &gt;= 30 Then<br>\ngrade = &#8220;D&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 50 And mark &gt;= 40 Then<br>\ngrade = &#8220;C-&#8220;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 60 And mark &gt;= 50 Then<br>\ngrade = &#8220;C&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 70 And mark &gt;= 60 Then<br>\ngrade = &#8220;C+&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt; 80 And mark &gt;= 70 Then<br>\ngrade = &#8220;B&#8221;<br>\nCells(2, 1).Value = grade<br>\nElseIf mark &lt;= 100 And mark &gt;=80 Then<br>\ngrade = &#8220;A&#8221;<br>\nCells(2, 1).Value = grade<br>\nEnd If<br>\nEnd Sub<\/p>\n\n\n\n<p>The output is shown in Figure 12.1<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.vbtutor.net\/Images\/progra_jan_23_2008_vbaIII_1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 12.1<\/strong><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-11-date-time-functions\/\">&lt;&lt;Lesson 11&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-13-select-case\/\">&lt;&lt;Lesson 13&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 11&gt;&gt; [Contents] &lt;&lt;Lesson 13&gt;&gt; In this lesson, you will learn how to create an Excel VBA code that can make a decision. Decision-making process is an important part of Excel VBA &nbsp;programming because it can help to solve practical problems intelligently so that it can provide useful feedback to the users. In Excel VBA, &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-12-if-then-else\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 12: 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-288","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 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel 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\/vba_lesson12.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 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel VBA\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson12.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:52:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"323\" \/>\n\t<meta property=\"og:image:height\" content=\"162\" \/>\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-12-if-then-else\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson12.htm\",\"name\":\"Excel VBA Lesson 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson12.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson12.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg\",\"datePublished\":\"2013-02-27T07:31:14+00:00\",\"dateModified\":\"2020-04-23T10:52:48+00:00\",\"description\":\"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel VBA\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson12.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson12.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson12.htm#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\/vba_lesson12.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 12: 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 VBA Lesson 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel 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\/vba_lesson12.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel VBA","og_url":"https:\/\/excelvbatutor.com\/vba_lesson12.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:52:48+00:00","og_image":[{"width":323,"height":162,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.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-12-if-then-else\/","url":"https:\/\/excelvbatutor.com\/vba_lesson12.htm","name":"Excel VBA Lesson 12: Using If.....Then....Else - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson12.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson12.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table4.1.jpg","datePublished":"2013-02-27T07:31:14+00:00","dateModified":"2020-04-23T10:52:48+00:00","description":"This Excel VBA lesson illustrates the usage of If...then...Else in controlling program flow in Excel VBA","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson12.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson12.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson12.htm#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\/vba_lesson12.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 12: 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\/288","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=288"}],"version-history":[{"count":31,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/288\/revisions"}],"predecessor-version":[{"id":3457,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/288\/revisions\/3457"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}