一 理解分支条件语句
1.1 VBA里的3种语句
- 顺序执行的block
- 分支判断语句(典型的就是指if)--- 按顺序判断,但不按顺序执行,只选择性执行,选择执行1个分支
- 循环语句
1.2 理解分支条件语句block内部,也是从上往下执行
- 分支语句是有序的
- 是从上往下执行的
- 所以很多判断逻辑,都得基于这个设计基础考虑
1.3 多层条件分支语句的嵌套(先小后大)
- 第1层(最外层):最特殊,最严格,最小范围
- 第2层:次特殊,次严格,范围较小
- 。。。。
- 第N-1层(最内层):最普通,最宽泛不严格,范围很大
- 第N层:补集,一定是补计划,和前面的组成一个完整的集合!
- If elseif 嵌套
- 先判断外层的小范围,再判断更大的范围
- 先特殊后一般
- 多个独立if
- If
- if
- 是独立判断
if 嵌套例子1
- x>1000 最具体,范围最小,放在最外层先判断
Sub test3001()
x = Application.RandBetween(1, 2000)
Debug.Print "x=" & x & " ";
If x >= 1000 Then
Debug.Print "x > 1000"
Debug.Print "到分支1了"
ElseIf x >= 100 Then
Debug.Print "x => 100"
Debug.Print "到分支2了"
ElseIf x >= 10 Then
Debug.Print "x => 10"
Debug.Print "到分支3了"
Else
Debug.Print "其他"
End If
End Sub
if 嵌套例子2
- x<10 最具体,范围最小,放在最外层先判断
x = Application.RandBetween(1, 1000)
Debug.Print "x=" & x & " ";
If x <= 10 Then
Debug.Print "x <= 10"
Debug.Print "到分支1了"
ElseIf x <= 100 Then
Debug.Print "x <= 100"
Debug.Print "到分支2了"
ElseIf x <= 1000 Then
Debug.Print "x <= 1000"
Debug.Print "到分支3了"
Else
Debug.Print "其他"
End If
End Sub
1.4 一个完整的if分支判断只会执行1个分支,
- 一个完整的if endif 判断,每次只会执行1个分支,
- 也就是说完整的if endif 判断,是按顺序判断,但不是顺序执行,而是选择性执行,只执行1个分支
- 而多个if,则是独立的,每次都会运行。效率也会低一些。
- 如果强行把一个逻辑判断,写成多个if,会很费。
Sub test3003() '如果多个if,独立判断多次,没过滤每个if都执行,效率低
x = Application.RandBetween(1, 2000)
Debug.Print "x=" & x
If x >= 1000 Then
Debug.Print "x => 1000"
Debug.Print "到if1了"
End If
If x >= 100 Then
Debug.Print "x => 100"
Debug.Print "到if2了"
End If
If x >= 10 Then
Debug.Print "x => 10"
Debug.Print "到if3了"
End If
这个结果如下(作为反例比较)
- 既没做到分支判断的目的
- 每个if 都执行了一遍
2 if 条件分支语句
2.1 基础语法
- 语法
- if condition then
- elseif condition then
- else
- end if
Sub t6(i)
If i > 90 Then
Debug.Print ("优秀")
ElseIf i >= 60 Then
Debug.Print ("通过")
Else
Debug.Print ("不及格")
End If
End Sub
Sub t7()
a = InputBox("请输入您的成绩")
t6 (a)
End Sub
2.2 语法特例:如果if then 只有1个语句,不需要end if
Sub testif1()
If a = 0 Then Debug.Print "a=0"
a = a + 1
If a = 1 Then
Debug.Print "a=1"
End If
End Sub
3 select case条件分支语句
3.1 基本语法
- select case 变量/对象
- case condition
- case condition
- case else
- 记住else 前必须带 case else 不能是单独的else
- end select
3.2 第2语法(用得少)
- select case 判断语句
- case true
- case false
- case else
- end select
Sub test9901(a, b)
Select Case a > b
Case True
Debug.Print "a>b"
Case False
Debug.Print "a<b"
End Select
End Sub
Sub test9902()
Call test9901(10, 2)
Call test9901(2, 10)
End Sub
3.2 表达式的写法
- 表达式必须前面带 is (类似like case like)
- 如果是两个表达式,需要写成 case is > , is < 中间逗号风格
- 等于某个值 case 90
- 大于 case is > 90
- 在某个范围内,下限 to 上限 case 4 to 8
- 多个条件,逗号分隔(表示任意一个条件满足即可,是或的关系)
- Case 1 to 4,7 to 9,11,13
Sub t6(i)
Select Case i
Case Is >= 90
Debug.Print "优秀"
Case Is < 89, Is >= 60
Debug.Print "合格"
Case Else
Debug.Print "不及格"
End Select
End Sub
Sub t7()
a = InputBox("请输入您的成绩")
t6 (a)
End Sub
Sub test801()
Select Case Cells(179, 1).Value
Case 0
Debug.Print "value is 0"
Case Is > 0
Debug.Print "value is positive"
Case Is < 0
Debug.Print "value is negetive"
End Select
End Sub
4 if 和 select 的区别
- select…Case语句,只能执行分支中的一个语句,但是,对各个语句的判断内容是没有限制的,也即判断的内容可以overlap,这一点和IF有绝对区别的。?
- select和IF分支结构基本上可以互相转换的,他们的主要区别是Select分支结构只能对同一个表达式的值进行分支判断,而IF却不受该限制。
参考
http://www.cnblogs.com/wuzhiblog/p/vba_three.html
https://blog.youkuaiyun.com/bangemantou/article/details/70194123