- 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-Else 語句
If 語句由布林表示式及一個或多個語句組成。如果條件為 True,則執行 If 條件下的語句。如果條件為 False,則執行 Else 部分下的語句。
語法
以下是 VBScript 中 If Else 語句的語法。
If(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 = 324
If x > y Then
MsgBox "X is Greater than Y"
Else
Msgbox "Y is Greater than X"
End If
End Sub
執行上述程式碼後,將生成以下結果。
Y is Greater than X
vba_decisions.htm
廣告
