One of the most common coding methods is the construction of a logic that checks for the presence of different conditions. As a programmer, whether a beginner or an experienced programmer, I hope you agree with me that this is one of the most common coding tasks.
In VBA, where statements are often used to construct such logic. For example, you can build a logic that checks if today is nice weather.
If that’s the case, you may leave your house. If not, check if it’s a light rain or blizzard. If it rains a little and your coat is ready to rain, you can leave. It’s a simple script, but it reminds you of common encoding problems.
In principle, you can maintain this logic by using an IF operator with multiple conditions. An alternative, which is useful under certain conditions, is the use of the Select Case statement. In this tutorial we will focus on the If statement with multiple conditions, but we will also briefly describe the basics of the Select Case statement and when to use it.
alt=card indicating whether width=582 height=286 data-ezsrc=http://184.108.40.206/wp-content/uploads/2020/10/VBA-If-Statements-with-Multiple-Conditions.png data-ez= /> should be exceeded or not.
Syntax table of the FI balance sheet
|If then||If the [state is right], then [do something] The end is when||If Good_Weather = True, then MsgBox Go Out ends when|
|If endless If (only one line)||If [the condition is good], then [do something].||If the weather’s good, MsgBox Go Out…|
|If Elsa||If the [state is right] Then [do something] Otherwise [do something] The end If||If Rain_Coat_Ready = Correct, then MsgBox Go Out or MsgBox Stay Home ends when|
|If not…||If [state 1 is correct], then [do something] If not [state 2 is correct], then [do something] The end If||If Good_Weather = true, then Msgbox Outbound ElseIf Good_Weather = false AND Rain_Coat_Ready = true, then Msgbox Outbound ends when|
|ElseIf and ElseIf (must be in this order)||If [state 1 is correct], then [do something] If not [state 2 is correct], then [do something] The end If||If Good_Weather = True, the output Otherwise If Good_Weather = False AND Rain_Foul_Ready = True, the output Otherwise Msgbox stays at home if Miss Box stays at home if|
|X||Y||AND THE RESULT||OR RESULT|
Example 1: Do I have to go out today?
We will develop the logic of the above diagram in phases.
Sub ShouldIGoOutToday()’Set the weather to good or badGood_Weather = False’Set the rain layer to ready or notRegen_Coat_Ready = True’Set the bad weather typeSbad_Weather = True Dan’If the weather is good, thenMsgBox ExitAnders’In bad weather = a little rain Dan’In bad weather = a little rainWhen ready to rain = Correct, Dan’in bad weather a little rain and raincoat readyMsgBox ExitAnders’in bad weather a little rain and raincoat not readyMsgBox Stay at homeEnd If bad_weather = blizzard Dan’in bad_weather = blizzardMsgBox Stay at homeEnd If End Under Data :;base64,%3csvg%20xmlns= alt= Example 1 vba code width=644 height=409 data-ezsrc=http://220.127.116.11/wp-content/uploads/2020/10/1604136815_93_VBA-If-Statements-with-Multiple-Conditions.png data-ez= />
Example 2: Use of AND/OR
You can specify more than one condition in the same state using EN keys or keys. Instead of Condition = True, you can check Condition1 = True and Condition2 = True. This method can be applied to the above example by checking if the weather is a little rainy and if the rain layer is ready.
Sub ShouldIGoOutTodayEx2() ‘Set the weather to good or bad Weather = False ‘Set the rain on your jacket ready or notRain_Coat_Ready = True ‘Set the bad weather to typeBas_Weather = Some RainIf Good_Weather = True Dan ‘if the weather is good, Then ‘If the weather is badIf the weather is badIf the weather is badIf the weather is badIf the weather is badIf the weather is badIf the weather is bad it’s a little rain and rain_Ready If the weather is bad it’s a little rain and rain your jacket is ready, then the Go OutMsgBox Go OutElseIf Bad_Weather = Snowstorm or Rain_Coat_Ready = False Then ‘If the weather is bad it’s snowstorm or rain on your jacket is not ready, then stay at homeMsgBox Stay at homeEnd subdata :;base64,%3csvg%20xmlns= alt=Code Research 2 width=680 height=334 data-ezsrc=http://18.104.22.168/wp-content/uploads/2020/10/1604136817_103_VBA-If-Statements-with-Multiple-Conditions.png data-ez= />
For more information on the use of OR declarations with If declarations, see this article :
Alternative to application If: Choose case
The most obvious way to choose a case is to do something if we are only interested in the value of a certain variable, and what we do depends on the value of that variable.
In the example above, there is not a single variable on which we depend to determine the outcome. We will try to implement the logic described above with Select Case.
The main difference between the Select Case and If operators is that the Select Case operator only allows something based on the value of the Select Case variable. To overcome this obstacle, you can set the Select Case variable to True. After the keyword Case, specify the complete condition and what we want to do if it appears in the next line.
Therefore, instead of this syntax:
If X = 2 and Y = 3, then…
It ends when
If you choose an option, it is the syntax :
Pick the truth.
Case X = 2 and Y = 3
End of the selection
The full implementation of the above examples using Select Case is shown below:
‘Set weather to good or bad
Good_Weather = False
‘Set rain on fur ready or not
Rain_Fur_Ready = False
‘Set bad weather type
Bad weather = Any kind of rain
Pick the truth.
Case Good_Weather = True
MsgBox Go Out
Case Good_Weather = Bad and bad_Weather = A little rain and a raincoat_Ready = Correct
MsgBox Go Out
Case Good_weather = Bad and bad_weather = A little rain and a raincoat_ready = Bath
MsgBox stay at home
Case Good_Weather = bad and bad_weather = blizzard
MsgBox stay at home
alt=Select case code width=740 height=378 data-ezsrc=http://22.214.171.124/wp-content/uploads/2020/10/1604136818_788_VBA-If-Statements-with-Multiple-Conditions.png data-ez= />
If the explanations are ideal for working with simple and complex logic, those different conditions and things we want to do in any case. On the other hand, Select Case can also be used as an alternative, but it is generally intended for a situation where we want to select one thing from several possible cases. As the logic becomes more complex and multiple levels are added to the logic, the selection process becomes less intuitive.