Excel VBA----之判断语句select
1、select case 语句 :
根据表达式的值来决定执行语句中的其中一个。
2、在VBE中编写代码:
Sub select单条件()
i = 1
Select Case i
Case Is > 0
Debug.Print ("i>0")
Case i < 0
Debug.Print ("i<0")
Case i = 0
Debug.Print ("i=0")
End Select
End Sub

3、执行代码,得到运行结果。

4、Excel界面如下:
功能为:通过输入血型,判断结果;例如输入血型为A,得到相应的结果;

5、在VBE中编写代码如下:
Sub select多条件()
Dim blood As String
Dim res As String
blood = Sheet1.[a13].Value
Select Case blood
Case "A"
Debug.Print ("为A型血")
Sheet1.[b13].Value = "为A型血"
Case "B"
Debug.Print ("为B型血")
Sheet1.[b13].Value = "为B型血"
Case "O"
Debug.Print ("为O型血")
Sheet1.[b13].Value = "为O型血"
Case "AB"
Debug.Print ("为AB型血")
Sheet1.[b13].Value = "为AB型血"
Case Else
Debug.Print ("不存在该血型")
Sheet1.[b13].Value = "不存在该血型"
End Select
End Sub

6、运行该代码,得到结果;
即当血型为A时,窗口输出病在Excel界面的相应表格中输出结果。


7、在Excel界面中,插入形状---右键形状---指定宏,设置刚刚添加的宏;
7、点击该形状,则调用刚刚的宏。

8、综上所述,本文从单条件、多条件两种情况对select关键字进行案例说明,并结合了Excel界面进行呈现。