30. VBA(三)流程控制-条件选择
2019-10-15 本文已影响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