xxxxxxxxxx
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
ElseIf condition_n Then
result_n
Else
result_else
End If
xxxxxxxxxx
Sub PositiveNegativeZero()
Dim number As Integer
number = InputBox("Enter a number:")
If number > 0 Then
MsgBox "The number is positive."
ElseIf number < 0 Then
MsgBox "The number is negative."
Else
MsgBox "The number is zero."
End If
End Sub
the first If condition checks if the number is greater than 0. If it is, a message box displays "The number is positive." If not, the ElseIf statement checks if the number is less than 0. If it is, a message box displays "The number is negative." If neither of these conditions are met, the Else statement executes and a message box displays "The number is zero."
To create an inline IF statement (akin to the tertiary operator "? :" we're all used to)
You must follow this syntax
xxxxxxxxxx
Dim example as String
example = IIF(<expression>, TRUE, FALSE)
to remember it, IIF stands for "INLINE IF"
ElseIf statement allows you to test additional conditions if the first If condition and any subsequent ElseIf conditions are not met. The syntax for the ElseIf statement is as follows:
xxxxxxxxxx
If condition1 Then
' code to execute if condition1 is True
ElseIf condition2 Then
' code to execute if condition2 is True
ElseIf condition3 Then
' code to execute if condition3 is True
Else
' code to execute if none of the conditions are True
End If