VBA basics
21. Conditional branch (ElseIf)

Excel macro basics and applications, introduction to Excel VBA
last updated:08-25-2019

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.


The statement is an If statement.

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.

Description of ElseIf

ElseIf condition Then
  the conditional expression is true

You can write this repeatedly.
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 you write this without using ElseIf,

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

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.



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)



  • >
  • >
  • 21. Conditional branch (ElseIf)

  • If you find this site useful, please share and bookmark.


    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.




    If you find this site useful, please share and bookmark.
    To the bottom of the text