If Then Else Statement
The If Then Else statement is a control structure that runs a block of code according to one or more expressions. It does not return a value.
Format A
If condition Then then_statement [Else else_statement]
If condition Then
statement_block
[ElseIf expression Then
statement_block ]...
[Else
statement_block ]
End If
If you require multiple statements in the Then clause or in the Else clause, then you must use Format B.
Arguments
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
condition |
Any expression that evaluates to TRUE (not zero) or FALSE (zero). |
then_statement |
Any valid single expression. |
else_statement |
Any valid single expression. |
expression |
Any expression that evaluates to TRUE (not zero) or FALSE (zero). |
statement_block |
Zero or valid expressions where a colon (:) or a different code line separates each expression. |
Example
The following example examines the time and the day of the week and returns a message:
Sub Button_Click
Dim h, m, m2, w
h = hour(now)
If h > 18 then
m = "Good evening, "
Elseif h >12 then
m = "Good afternoon, "
Else
m = "Good morning, "
End If
w = weekday(now)
If w = 1 or w = 7
Then m2 = "the office is closed."
Else m2 = "please hold for company operator."
End If
End Sub