- 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 - 巢狀 If 語句
另一個 If 或 ElseIf 語句中的 If 或 ElseIf 語句。內部 If 語句根據最外部的 If 語句執行。這使 VBScript 能夠輕鬆處理複雜條件。
語法
以下是 VBScript 中巢狀 **If** 語句的語法。
If(boolean_expression) Then
Statement 1
.....
.....
Statement n
If(boolean_expression) Then
Statement 1
.....
.....
Statement n
ElseIf (boolean_expression) Then
Statement 1
.....
....
Statement n
Else
Statement 1
.....
....
Statement n
End If
Else
Statement 1
.....
....
Statement n
End If
示例
為了演示目的,讓我們藉助函式查詢一個正數的型別。
Private Sub nested_if_demo_Click()
Dim a As Integer
a = 23
If a > 0 Then
MsgBox "The Number is a POSITIVE Number"
If a = 1 Then
MsgBox "The Number is Neither Prime NOR Composite"
ElseIf a = 2 Then
MsgBox "The Number is the Only Even Prime Number"
ElseIf a = 3 Then
MsgBox "The Number is the Least Odd Prime Number"
Else
MsgBox "The Number is NOT 0,1,2 or 3"
End If
ElseIf a < 0 Then
MsgBox "The Number is a NEGATIVE Number"
Else
MsgBox "The Number is ZERO"
End If
End Sub
執行以上程式碼後,它會生成以下結果。
The Number is a POSITIVE Number The Number is NOT 0,1,2 or 3
vba_decisions.htm
廣告
