EXCEL VBA中 if条件语句的介绍

时间:2026-02-13 21:07:34

1、'单行写法  (后面将省去首尾两句)

Sub 条件语句()

If a < 20 Then [b2] = "good"

end sub

2、'多行写法

If a < 20 Then

[b2] = "shit"

End If

'if esle

If a < 20 Then

[b2] = "yes"

Else

[b2] = "no"

End If

3、'多条件判断

If a < 20 Then

 [b2] = "very bad"

ElseIf a < 40 Then

 [b2] = "bad"

ElseIf a < 60 Then

 [b2] = "normal"

Else

 [b2] = "good"

End If

4、单行写法if后,执行多条语句,用冒号隔开

If a < 20 Then [b2] = "good": [b3] = "yes"

'单行if else

If a < 20 Then [b2] = "aa" Else [b2] = "bb"

5、总结:

if 条件1 then

  条件1成立时运行的语句

elseif 条件2 then

 条件2成立运行的语句

....

else

  所有条件都不成立时运行的语句

end if

© 2026 海能知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com