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= data-ez= /> should be exceeded or not.

Syntax table of the FI balance sheet

Description Format Example
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

Logic port


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= 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= data-ez= />

For more information on the use of OR declarations with If declarations, see this article :

How to Use IF OR Statements in VBA

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…

Do something.

It ends when

If you choose an option, it is the syntax :

Pick the truth.

Case X = 2 and Y = 3

Do something.

End of the selection

The full implementation of the above examples using Select Case is shown below:

Sub ShouldIGoutTodayEx3()
‘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

End Select
End Sub
alt=Select case code width=740 height=378 data-ezsrc= 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.

You May Also Like

2 Ways To Transfer Files And Apps Data From An Android To OnePlus 6T

Learning 2 An easy way to transfer contacts, messages, photos, applications, data…

Javascript toLowerCase Function

Javascript is a widely used scripting or programming language. Programmers often use…

How to Watch SonyLiv Free Online [100% Working Trick]

Last update in 3 days from exotic Iran. How can you watch…

Salesforce Updates to Improve Employee Engagement was originally conceived as a series of new and existing Salesforce…