美文网首页
30. VBA(三)流程控制-条件选择

30. VBA(三)流程控制-条件选择

作者: 神农架村姑 | 来源:发表于2019-10-15 12:33 被阅读0次

    1. IF

    If...Then...ElseIf...Then...End If

    If a>90 Then
        ...
    ElseIf a>80 Then
        ...
    Else
        ...
    End If
    

    2. Select Case

    Sub test2()
        Dim score As Integer
        score = 53
        Select Case score
            Case Is >= 90
                Debug.Print "优"
            Case Is >= 80
                Debug.Print "良"
            Case Is >= 60
                Debug.Print "中"
            Case Else
                Debug.Print "差"
        End Select
    End Sub
    

    如果需要在Case条件中使用闭合区间,即同时出现大于且小于的情况,如上例中,大于60且小于70。则需要使用两个Is语句,中间用逗号隔开:

    Sub test2()
        Dim score As Integer
        score = 53
        Select Case score
            Case Is >= 90
                Debug.Print "优"
            Case Is >= 80
                Debug.Print "良"
            Case Is >= 60, Is <= 70
                Debug.Print "中"
            Case Else
                Debug.Print "差"
        End Select
    End Sub
    

    相关文章

      网友评论

          本文标题:30. VBA(三)流程控制-条件选择

          本文链接:https://www.haomeiwen.com/subject/itxsmctx.html