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

流程图

VBScript if statement

示例

为了演示的目的,让我们借助一个函数找出 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
广告
© . All rights reserved.