语句一:if判断语句
Sub 判断1() ‘单条件判断 If Range("a1").Value > 0 Then Range("b1") = "正数" Else Range("b1") = "负数或0" End If End Sub Sub 判断2() ‘多条件判断 If Range("a1").Value > 0 Then Range("b1") = "正数" ElseIf Range("a1") = 0 Then Range("b1") = "等于0" ElseIf Range("B1") <= 0 Then Range("b1") = "负数" End If End Sub Sub 多条件判断2() If Range("a1") <> "" And Range("a2") <> "" Then Range("a3") = Range("a1") * Range("a2") End If End Sub
语句二:IIF函数判断
Sub 判断4() Range("a3") = IIf(Range("a1") <= 0, "负数或零", "负数") End Sub
语句三:select判断
Sub 判断1() ‘单条件判断 Select Case Range("a1").Value Case Is > 0 Range("b1") = "正数" Case Else Range("b1") = "负数或0" End Select End Sub Sub 判断2() ‘多条件判断 Select Case Range("a1").Value Case Is > 0 Range("b1") = "正数" Case Is = 0 Range("b1") = "0" Case Else Range("b1") = "负数" End Select End Sub Sub 判断3() If Range("a3") < "G" Then MsgBox "A-G" End If End Sub
语句四:判断范围
Sub if区间判断() If Range("a2") <= 1000 Then Range("b2") = 0.01 ElseIf Range("a2") <= 3000 Then Range("b2") = 0.03 ElseIf Range("a2") > 3000 Then Range("b2") = 0.05 End If End Sub Sub select区间判断() Select Case Range("a2").Value Case 0 To 1000 Range("b2") = 0.01 Case 1001 To 3000 Range("b2") = 0.03 Case Is > 3000 Range("b2") = 0.05 End Select End Sub
总结:VBA常见判断方法和函数
时间: 2024-10-07 06:00:13