VBA if...elseif...else語句

if 語句之後有一個或多個elseif 語句,由布爾表達式,接着是默認else語句,其中,當所有的條件爲假時執行 else 語句。

語法 :

if elseif else語法在VBScript 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