If then formula multiple conditions
When the logic gets more complex and multiple levels get added to the logic, the Select Case becomes less intuitive.As always, you have posted something very useful, Chandoo. On the other hand, Select Case could also be used as an alternative, but it’s usually meant for a situation where we want to select a single thing to do out of a number of potential things. If statements are ideal for handling simple and complex logic that involves multiple conditions and things we want to happen accordingly.
#If then formula multiple conditions full#
The full implementation of the above examples using Select Case is shown below: Sub ShouldIGoOutTodayEx3()Ĭase Good_Weather = False And Bad_Weather = "Some Rain" And Rain_Coat_Ready = TrueĬase Good_Weather = False And Bad_Weather = "Some Rain" And Rain_Coat_Ready = FalseĬase Good_Weather = False And Bad_Weather = "Blizzard" You would then specify the full condition after the keyword Case and the thing we want to do when it is met on the next line. In order to overcome this hurdle, you can set the Select Case variable to True. The main difference between the Select Case and If statements is that Select Case allows you to only ‘do something’ based on the value of the Select Case variable. We will attempt to implement the above logic using Select Case. In our above example, there is no single variable that we depend on to decide the outcome. The most obvious way to select case is to do something when we are only interested in the value of a particular variable and the things we do will depend on the value of that variable. Alternative to If Statement: Select Case 'if the bad weather is blizzard or the rain coat is not ready then Stay Homeįor more on using AND and OR with If statements, see this article: 'If the bad weather is some rain AND the rain coat is ready then Go OutĮlseIf Bad_Weather = "Blizzard" Or Rain_Coat_Ready = False Then If Bad_Weather = "Some Rain" And Rain_Coat_Ready = True Then This can be applied to the above example by checking if the bad weather is ‘some rain’ AND the rain coat is ready. Instead of checking that condition = True, we could check that condition1 = True AND condition2 = True. You could specify multiple conditions within the same condition by using AND and OR. 'if the bad weather is some reain and the rain coat is not ready
'If the bad weather is some rain and the rain coat is ready We will develop the logic for the above diagram step-by-step. If Good_Weather = True Then Msgbox “Go Out” ElseIf Good_Weather = False AND Rain_Coat_Ready = True Then Msgbox “Go Out” Else Msgbox “Stay Home” End if If Good_Weather = True Then Msgbox “Go Out” ElseIf Good_Weather = False AND Rain_Coat_Ready = True Then Msgbox “Go Out” End if If Rain_Coat_Ready = True Then MsgBox “Go Out” Else MsgBox “Stay Home” End If If Good_Weather = True Then MsgBox “Go Out” End If In this guide, we will focus on the If Statement with multiple conditions, but we will also briefly mention the basics of Select Case statement as well as when to use it. An alternative that makes sense under certain conditions is to use the Select Case statement. We mostly can handle such logic by using the IF statement with multiple conditions. This is a simple scenario, but it resembles a lot of common coding problems. If it is slightly raining and you have your rain coat ready, then you can leave. If it is not, then check if it’s slightly raining or if it’s a blizzard. For example, you could build logic that checks if the weather is good today. In VBA, If statements are commonly used to build such logic. As a coder, whether novice or experienced, hopefully you agree that this is one of the most common coding tasks. One of the most common things to do in coding is to build logic that checks for the occurrence of multiple conditions.