VBA - If Elseif - Else 语句
一条 If 语句后跟一个或多个由布尔表达式组成的 ElseIf 语句,然后跟一个默认的 else 语句,该语句在所有条件都变为 false 时执行。
语法
以下是 VBScript 中 If Elseif - Else 语句的语法。
If(boolean_expression) Then Statement 1 ..... ..... Statement n ElseIf (boolean_expression) Then Statement 1 ..... .... Statement n ElseIf (boolean_expression) Then Statement 1 ..... .... Statement n Else Statement 1 ..... .... Statement n End If
流程图
示例
出于演示目的,让我们借助函数来查找 Excel 中两个数字之间的最大值。
Private Sub if_demo_Click()
Dim x As Integer
Dim y As Integer
x = 234
y = 234
If x > y Then
MsgBox "X is Greater than Y"
ElseIf y > x Then
Msgbox "Y is Greater than X"
Else
Msgbox "X and Y are EQUAL"
End If
End Sub
执行上述代码时,会产生以下结果。
X and Y are EQUAL

