一、单行 If 语句
 
If x > 10 Then MsgBox "x is greater than 10"
二、多行 If...Then...End If 语句
 
If x > 10 Then
    MsgBox "x is greater than 10"
    y = x + 5
End If三、If...Then...Else 语句
 
If condition Then
    ' 当条件为真时执行的代码块
    statement1
Else
    ' 当条件为假时执行的代码块
    statement2
End If四、If...Then...ElseIf...Else 语句
 
If condition1 Then
    ' 当 condition1 为真时执行的代码块
    statement1
ElseIf condition2 Then
    ' 当 condition1 为假且 condition2 为真时执行的代码块
    statement2
Else
    ' 当所有条件都为假时执行的代码块
    statement3
End If示例:

按钮指定函数:

sub关键字的简单理解
Sub 是一个关键字,用于定义一个子程序(Subroutine)。子程序是一组可以执行特定任务的VBA语句集合。
Sub 子程序不返回值(与 Function 过程不同,后者可以返回一个值)。
五、Select Case 语句
 
当需要检查多个可能的值时,Select Case 语句更为简洁和高效。
Select Case expression
    Case value1
        ' 当 expression = value1 时执行的代码块
        statement1
    Case value2
        ' 当 expression = value2 时执行的代码块
        statement2
    ...
    Case Else
        ' 当所有 Case 都不匹配时执行的代码块
        statementN
End Select示例:
Select Case x
    Case 1 To 10
        MsgBox "x is between 1 and 10"
    Case 11 To 20
        MsgBox "x is between 11 and 20"
    Case Else
        MsgBox "x is out of range"
End Select



















