21. Conditional branch (ElseIf)
Some of this topic was processed by machine translation. Original
In the previous IF statement, there were two choices: true or false.
This time, we will write in the case of multiple-conditional branching with an If statement that requires more branches.
If statement ElseIf syntax
If Condition1 Then
Condition1 is true processing
[ElseIf Condition2 Then
Condition2 is true processing]
[ElseIf Condition3 Then
Condition1 is true processing]
・・・
[Else
All conditional expressions are false]
End If
[ ] Is optional.
Condition1 is true processing
[ElseIf Condition2 Then
Condition2 is true processing]
[ElseIf Condition3 Then
Condition1 is true processing]
・・・
All conditional expressions are false]
End If
[ ] Is optional.
Description of ElseIf
ElseIf condition Then
the conditional expression is true
You can write this repeatedly.
For example
If you write this without using ElseIf,
These two VBA codes are the same thing.
In the case of multiple limbs, using ElseIf makes it much easier to see.
However, if possible, use Select Case rather than ElseIf.
The next time you use Select Case, the VBA code will be easier to read.
the conditional expression is true
For example
If Condition1 Then
Condition1 is true processing
ElseIf Condition2 Then
Condition2 is true processing
ElseIf Condition3 Then
Condition3 is true processing
Else
All conditional expressions are false
End If
If Condition1 Then
Condition1 is true processing
Else
If Condition2 Then
Condition2 is true processing
Else
If Condition3 Then
Condition3 is true processing
Else
All conditional expressions are false
End If
End If
End If
In the case of multiple limbs, using ElseIf makes it much easier to see.
However, if possible, use Select Case rather than ElseIf.
The next time you use Select Case, the VBA code will be easier to read.
Articles with the same theme "VBA basics"
18. Get last row (End, Rows.Count)
19. General practice question 1
20. Conditional branch (If)
21. Conditional branch (ElseIf)
22. Conditional branch (Select Case)
23. Message box (MsgBox function)
24. Input box (InputBox function)
25. About named arguments
26. General practice question 2
27. Select book / sheet (Select, Activate)
28. Select cell / row / column (Select, Activate)
I'm going to pay close attention to the description,
If you have any mistakes or suggestions,「Contact Us」I hope you will let me know.
Use the posted VBA code at your own risk.
We are not liable for any damages such as data corruption.