(1)IF判断语句
单条件判断
Sub 单条件判断() If Range("a1") > 0 Then Range("b1") = "正数" Else Range("b1") = "负数或0" End IfEnd Sub
多条件判断1
Sub 多条件判断1() If Range("a1") >0 Then Range("b1") = "正数" ElseIf Range("a1") = 0 Then Range("b1") = "等于0" Else Range("b1") = "负数" End IfEnd Sub
多条件判断2
Sub 多条件判断2() If Range("a1") <>"" And Range("a2") <>"" Then Range("a3") = Range("a1") * Range("a2") End IfEnd Sub
区间判断
Sub IF区间判断() If Range("a1") >3000 Then Range("b1") = 0.01 ElseIf Range("a1") > 1000 Then Range("b1") = 0.03 Else Range("b1") = 0.05 End IfEnd Sub
注意:以上均为数值比较,还可用字母比较,如下.
字母判断
Sub 字母判断() If Range("a1") < "G" Then MsgBox "A-G" End IfEnd Sub
(2)Select判断
单条件判断
Sub 单条件判断() Select Case Range("a1").Value Case Is > 0 Range("b1") = "正数" Case Else Range("b1") = "负数或0" End SelectEnd Sub
多条件判断
Sub 多条件判断() Select Case Range("a1").Value Case Is > 0 Range("b1") = "正数" Case Is = 0 Range("b1") = "0" Case Else Range("b1") = "负数" End SelectEnd Sub
区间判断
Sub Select区间判断() Select Case Range("a1").Value Case 0 To 1000 Range("b1") = 0.05 Case 1001 To 3000 Range("b1") = 0.03 Case Is > 3000 Range("b1") = 0.01 End SelectEnd Sub
IF判断语句和Select判断语句的区别:
1.IF判断语句可同时判断多个条件,而Select判断语句需先选定一个对象再进行判断
2.IF判断语句使用较多,但Select判断语句看起来较为简洁
(3) IIF函数判断
Sub 函数判断() Range("b1") = IIF(Range("a1")<=0,"负数或零","正数")End Sub