Example 3:<\/h3>\n\n\n\nDim 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\nBe 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\nin 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\nOtherwise, 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\nLook at the following example:<\/p>\n\n\n\n
Example 4:<\/h3>\n\n\n\nDim 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\nIn the above example, we created three conditions:<\/p>\n\n\n\n
\n\u201eGood Morning!\u201d<\/em> for times between 12:00:00 AM and 11:59:59 AM.<\/li>\n\n\n\n\u201eGood Afternoon!\u201d<\/em> for times between 12:00:00 PM and 06:00:00 PM.<\/li>\n\n\n\n\u201eGood Evening!\u201d<\/em> for times between 06:00:01 PM and 11:59:59 PM.<\/li>\n<\/ol>\n\n\n\nSelect 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\nExample 5:<\/h3>\n\n\n\nDim 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\ndayOfWeek<\/em> returns the number of days for the selected date. In our case, it will be Monday.<\/p>\n\n\n\nExample 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\nDim 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\nIf 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":"\n
Conditional Statements<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n