- VBA 教程
- VBA - 主頁
- VBA - 概述
- VBA - Excel 宏
- VBA - Excel 術語
- VBA - 宏註釋
- VBA - 訊息框
- VBA - 輸入框
- VBA - 變數
- VBA - 常量
- VBA - 運算子
- VBA - 決策
- VBA - 迴圈
- VBA - 字串
- VBA - 日期和時間
- VBA - 陣列
- VBA - 函式
- VBA - 子過程
- VBA - 事件
- VBA - 錯誤處理
- VBA - Excel 物件
- VBA - 文字檔案
- VBA - 程式設計圖表
- VBA - 使用者窗體
- VBA 有用資源
- VBA - 快速指南
- VBA - 有用資源
- VBA - 討論
VBA - 如果 Elseif - Else 語句
If 語句後跟一個或多個 ElseIf 語句,其中包含布林表示式,然後後跟一個預設 else 語句(在所有條件變為假時執行)。
語法
以下是 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
vba_decisions.htm
廣告
