位置:首页 » 文章/教程分享 » VBA if/else语句

if 语句由一个布尔表达式后跟一个或多个语句。if 条件为真,下面的 if 条件被执行的语句。if 条件为假,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