excel vba if then else
IF條件語句的基本結構如下
If 條件 Then
' 如果條件為真,執行這裡的程式碼
ElseIf 其他條件 Then
' 如果其他條件為真,執行這裡的程式碼
Else
' 如果以上條件都不為真,執行這裡的程式碼
End If

Step 1 在功能列「開發人員」中程式碼選擇「Visual Basic」

Step 2 複製下方範例程式碼貼上,並修改條件,按下執行即可
Sub TestIfStatement()
Dim x As Integer
x = Range("A1")
' 檢查 x 的值
If x > 5 Then
MsgBox "x 大於 5"
ElseIf x = 5 Then
MsgBox "x 等於 5"
Else
MsgBox "x 小於 5"
End If
End Sub
【教學】Excel VBA IF要怎麼使用?超簡單馬上學會!