• 【VBA编程】06.控制语句


    【IF...THEN...语句】

    If condition Then

    [statements1]

    else

    [statements2]

    end if

    condition 为一个逻辑表达式,表示做选择时需要判别的条件,其结果为布尔类型,当其值为真时,执行statements1语句,为假是则执行ELSE中statements2语句

    【代码区域】

    Private Sub 测试if()
     Dim age As Integer
     age = InputBox("请输入年龄", "输入年龄窗口")
     If (age >= 18) Then
        MsgBox "已经成年", vbOKOnly, "判断结果"
     Else
        MsgBox "未成年", vbOKOnly, "判断结果"
     End If
    End Sub

    【IF嵌套语句】

    IF condition Then 

    [statements1]

    [Elseif condition2 then statements2]

    [Elseif condition3 then statements3]

    ...

    [Elseif conditionN then statementsN]

    Else statements 

    End if

    【代码区域】

    Private Sub 测试if嵌套()
     Dim age As Integer
     age = InputBox("请输入年龄", "输入年龄窗口")
     If (age <= 6) Then
        MsgBox "童年", vbOKOnly, "判断结果"
     ElseIf (6 < age And age <= 17) Then
        MsgBox "少年", vbOKOnly, "判断结果"
     ElseIf (17 < age And age <= 40) Then
        MsgBox "青年", vbOKOnly, "判断结果"
     ElseIf (41 < age And age <= 65) Then
        MsgBox "中年", vbOKOnly, "判断结果"
     Else
        MsgBox "老年", vbOKOnly, "判断结果"
     End If
    End Sub

    【Select Case语句】

    Select Case testexpression

    [Case expressionlist - n]

    [statements-n]...

    [Case Else

    [elsestatements]]

    End Select

    【代码区域】

    Private Sub case测试()
    Dim age As Integer
    age = InputBox("请输入年龄", "输入年龄窗口")
    Select Case age
        Case 0 To 6
        MsgBox "童年", vbOKOnly, "判断结果"
        Case 6 To 17
        MsgBox "少年", vbOKOnly, "判断结果"
        Case 17 To 40
        MsgBox "青年", vbOKOnly, "判断结果"
        Case 40 To 65
        MsgBox "中年", vbOKOnly, "判断结果"
        Case Else
        End Select
    End Sub

    【Tips】

    在Select Case中需要注意上下界问题,例如:"Case 0 to 6"表示6>=age>0

  • 相关阅读:
    [C++] static member variable and static const member variable
    [C++] const inside class VS const outside class
    [C++] OOP
    [C++] Function Template
    [C++] right value reference
    [C++] advanced reference
    [C++] const and char*
    [C++] c Struct VS c++ Struct
    [C++] decltype(auto) C++ 11 feature
    easyui-validatebox 的简单长度验证
  • 原文地址:https://www.cnblogs.com/OliverQin/p/6206946.html
Copyright © 2020-2023  润新知