{"id":795,"date":"2018-06-30T17:49:49","date_gmt":"2018-06-30T17:49:49","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=795"},"modified":"2024-03-16T13:35:12","modified_gmt":"2024-03-16T13:35:12","slug":"conditional-statements","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/","title":{"rendered":"Conditional Statements"},"content":{"rendered":"\n

If you want your procedure to behave differently depending on the conditions it meets you have to use one of the conditional statements. In this lesson, I will show you how to write such statements.<\/p>\n\n\n\n

If Then Statement<\/h2>\n\n\n\n

If you use the IF..THEN<\/strong> statement, the code will be executed if the condition is met. Look at the following example.<\/p>\n\n\n\n

Example 1:<\/h3>\n\n\n\n
Dim myTime As Date\nDim timeToDouble As Double\nmyTime = #11:45:12 AM#\n \ntimeToDouble = TimeValue(myTime)\nIf myTime < 0.5 Then MsgBox (\"Good Morning!\")<\/pre>\n\n\n\n

Line 1, 2: <\/strong>Here, you have two variables: one declared as Date<\/strong> and the other one as Double.<\/strong><\/p>\n\n\n\n

Line 3: <\/strong>Time is assigned to the variable<\/a>.<\/strong><\/p>\n\n\n\n

Line 5: <\/strong>The myTime<\/em> value is converted to Double<\/strong> and assigned to the timeToDouble<\/em> variable.<\/p>\n\n\n\n

Line 6: <\/strong>The statement If myTime < 0.5<\/em> checks whether it is morning (more about storing dates in the lesson How Excel stores dates and times<\/a>). If the value is lower than 0.5 then Excel displays \u201eGood morning!\u201d. If the value is larger or equal to 0.5 then Excels does nothing.<\/p>\n\n\n\n

Example 2:<\/h3>\n\n\n\n
Dim myTime As Date\nDim timeToDouble As Double\nmyTime = #11:45:12 AM#\n\ntimeToDouble = TimeValue(myTime)\nIf myTime < 0.5 Then\n    MsgBox (\"Good Morning!\")\nEnd If<\/pre>\n\n\n\n
\n

In the second example the\u00a0IF..THEN\u00a0statement occupies two lines instead of one as it did in the previous example. In this case, you have to add\u00a0End If\u00a0in line 8, at the end of the statement.<\/p>\nNOTICE<\/cite><\/blockquote>\n\n\n\n

If Then Else Statement<\/h2>\n\n\n\n

With IF..THEN..ELSE<\/strong> structure Excel will execute the block of code if the condition is met and another block of code if the condition is not met. Look at the following example.<\/p>\n\n\n\n

Example 3:<\/h3>\n\n\n\n
Dim myTime As Date\nDim timeToDouble As Double\nmyTime = #12:45:12 PM#\n\ntimeToDouble = TimeValue(myTime)\n\nIf myTime < 0.5 Then\n    MsgBox (\"Good Morning!\")\nElse\n    MsgBox (\"Good Afternoon!\")\nEnd If<\/pre>\n\n\n\n

Be cautious when you use the\u00a0IF..THEN..ELSE\u00a0statements, because it is very easy to make an error. For example, you cannot write:<\/p>\n\n\n\n

If myTime < 0.5 Then MsgBox (\"Good Morning!\")<\/pre>\n\n\n\n

in one line, but you have to write this statement in two lines instead.<\/p>\n\n\n\n

If myTime < 0.5 Then\nMsgBox (\"Good Morning!\")<\/pre>\n\n\n\n

Otherwise, VBA will display the following error:<\/p>\n\n\n\n

If Then ElseIf Else Statement<\/h2>\n\n\n\n

In the previous lessons, we used an example to display one of the two messages: \u201eGood Morning!\u201d<\/em> for the times before noon or \u201eGood Afternoon!\u201d<\/em> for the times after noon. But what if we want to add the third condition to display \u201eGood evening\u201d<\/em>. In this case, we can use the ELSEIF<\/strong> statement.<\/p>\n\n\n\n

Look at the following example:<\/p>\n\n\n\n

Example 4:<\/h3>\n\n\n\n
Dim myTime As Date\nDim timeToDouble As Double\nmyTime = #6:45:12 PM#\n\ntimeToDouble = TimeValue(myTime)\n\nIf myTime > 0 And myTime < 0.5 Then\n    MsgBox (\"Good Morning!\")\nElseIf myTime >= 0.5 And myTime < 0.75 Then\n    MsgBox (\"Good Afternoon!\")\nElse\n    MsgBox (\"Good Evening!\")\nEnd If<\/pre>\n\n\n\n

In the above example, we created three conditions:<\/p>\n\n\n\n

    \n
  1. \u201eGood Morning!\u201d<\/em> for times between 12:00:00 AM and 11:59:59 AM.<\/li>\n\n\n\n
  2. \u201eGood Afternoon!\u201d<\/em> for times between 12:00:00 PM and 06:00:00 PM.<\/li>\n\n\n\n
  3. \u201eGood  Evening!\u201d<\/em> for times between 06:00:01 PM and 11:59:59 PM.<\/li>\n<\/ol>\n\n\n\n

    Select Case Statement<\/h2>\n\n\n\n

    If you have to create a lot of conditional statements, using IF<\/strong> statements is not the best idea. In such a situation, the best way to write a conditional statement will be using the SELECT..CASE<\/strong> construct.<\/p>\n\n\n\n

    Example 5:<\/h3>\n\n\n\n
    Dim dayOfWeek As Byte\n\ndayOfWeek = Weekday(\"12\/22\/2014\")\n\nSelect Case dayOfWeek\n    Case 1\n        MsgBox (\"Sunday\")\n    Case 2\n        MsgBox (\"Monday\")\n    Case 3\n        MsgBox (\"Tuesday\")\n    Case 4\n        MsgBox (\"Wednesday\")\n    Case 5\n        MsgBox (\"Thursday\")\n    Case 6\n        MsgBox (\"Friday\")\n    Case 7\n        MsgBox (\"Saturday\")\nEnd Select<\/pre>\n\n\n\n

    dayOfWeek<\/em> returns the number of days for the selected date. In our case, it will be Monday.<\/p>\n\n\n\n

    Example 6:<\/h3>\n\n\n\n

    In this example, I used the SELECT..CASE<\/strong> statement to specify the condition for the range between 2 and 6 and for the rest.<\/p>\n\n\n\n

    Dim dayOfWeek As Byte\n\ndayOfWeek = Weekday(\"12\/22\/2014\")\n\nSelect Case dayOfWeek\n    Case 2 To 6\n        MsgBox (\"Workday\")\n    Case Else\n        MsgBox (\"Weekend\")\nEnd Select<\/pre>\n\n\n\n

    If the returned day is between Monday<\/em> and Friday<\/em> the macro returns \u201eWorkday\u201d, otherwise it returns \u201eWeekend\u201d.<\/p>\n","protected":false},"excerpt":{"rendered":"

    If you want your procedure to behave differently depending on the conditions it meets you have to use one of the conditional statements….<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[188],"yoast_head":"\nConditional Statements<\/title>\n<meta name=\"description\" content=\"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the "IF..THEN" statement.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conditional Statements\" \/>\n<meta property=\"og:description\" content=\"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the "IF..THEN" statement.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-30T17:49:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-16T13:35:12+00:00\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Conditional Statements\",\"datePublished\":\"2018-06-30T17:49:49+00:00\",\"dateModified\":\"2024-03-16T13:35:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\"},\"wordCount\":506,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"keywords\":[\"added\"],\"articleSection\":[\"training\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\",\"url\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\",\"name\":\"Conditional Statements\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"datePublished\":\"2018-06-30T17:49:49+00:00\",\"dateModified\":\"2024-03-16T13:35:12+00:00\",\"description\":\"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the \\\"IF..THEN\\\" statement.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Conditional Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Conditional Statements","description":"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the \"IF..THEN\" statement.","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:\/\/officetuts.net\/excel\/training\/conditional-statements\/","og_locale":"en_US","og_type":"article","og_title":"Conditional Statements","og_description":"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the \"IF..THEN\" statement.","og_url":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/","article_published_time":"2018-06-30T17:49:49+00:00","article_modified_time":"2024-03-16T13:35:12+00:00","author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Conditional Statements","datePublished":"2018-06-30T17:49:49+00:00","dateModified":"2024-03-16T13:35:12+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/"},"wordCount":506,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"keywords":["added"],"articleSection":["training"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/","url":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/","name":"Conditional Statements","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"datePublished":"2018-06-30T17:49:49+00:00","dateModified":"2024-03-16T13:35:12+00:00","description":"Learn how to use conditional statement to make your procedure work differently based on meeting conditions. Check out the \"IF..THEN\" statement.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/training\/conditional-statements\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/training\/conditional-statements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Conditional Statements"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/795"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=795"}],"version-history":[{"count":8,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/795\/revisions"}],"predecessor-version":[{"id":17266,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/795\/revisions\/17266"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}