7.4 Controlling code execution
If...Then...[Else]
- If...Then...[Else], single-line version - where Else is optional
x = 3 If x = 3 Then MsgBox "Three" If x = 4 Then MsgBox "Four" Else MsgBox "Not Four"
- End If will cause an error in the single-line version
If x = 3 Then MsgBox "Three" ✅
If x = 3 Then MsgBox "Three" End If❌
If x = 4 Then MsgBox "Four" Else MsgBox "Not Four" End If❌ - If...Then...[Else], multiline (block) version
x = 3 If x = 3 Then MsgBox "Three" End If ' << Block If requires End If If x = 4 Then MsgBox "Four" Else MsgBox "Not Four" End If ' << Block If requires End If
- Block If must end with End If
- If...Then...[ElseIf], multi line version - where ElseIf is optional
x = 4 If x = 4 Then MsgBox "Four" End If ' === ElseIf multiline If x = 1 Then ' << If...Then must be the first line MsgBox "One" ElseIf x = 2 Then MsgBox "Two" ElseIf x = 3 Then MsgBox "Three" ElseIf x = 4 Then ' << TRUE MsgBox "Four" ElseIf x = 5 Then MsgBox "Five" Else MsgBox "Not in 1 to 5 range" End If ' << Block If requires End If
- For each multiline Block If structure, if the logical test (condition) is TRUE then the statements following Then are executed and control then passes to End If
- Revised: Saturday 25th of February 2023 - 09:37 AM, [Australian Eastern Time (AET)]