else也是可以写在同一行的,记得写在同一行的话是不用加end i

简介: else也是可以写在同一行的,记得写在同一行的话是不用加end if的,多个条件之间用英文的冒号(:)连接如:Sub if没有endif()a = 2If a

本例所举例子均在vba代码窗口中测试过需要的话可以直接复制代码测试Sub If语句用法()a = Int(Rnd() * 51 + 50) '随机生成50-100的整数MsgBox aIf a >= 90 Then MsgBox "优秀"If 80 <= a And a < 90 Then MsgBox "良好"If 60 <= a And a < 80 Then MsgBox "合格"If a < 60 Then MsgBox "不合格"End Sub单条if语句是不用写end if的,但then后面的语句需要写在同一行,如果then后面要运行多个语句可以用冒号(:)连接,如Sub if函数用法()a = Int(Rnd() * 51 + 50)MsgBox aIf a >= 90 Then MsgBox "优秀": MsgBox "很好,再接再厉"If 80 <= a And a < 90 Then MsgBox "良好": MsgBox "还不错,但还需努力"If 60 <= a And a < 80 Then MsgBox "合格": MsgBox "有点危险哦,不努力点会被喊家长哦"If a < 60 Then MsgBox "不合格": 喊家长End Sub上面例子If a < 60 Then MsgBox "不合格": 喊家长,如果a小于60则显示不合格消息框,然后调用子程序“喊家长”Sub 喊家长()MsgBox "老师,孩子考不好你就打他"End Sub如果分开写必须分行写并加上End If,上面的4个if,如果按标准写法要写4个end ifSub If语句用法()a = Int(Rnd() * 51 + 50)MsgBox aIf a >= 90 ThenMsgBox "优秀"End IfIf 80 <= a And a < 90 ThenMsgBox "良好"End IfIf 60 <= a And a < 80 ThenMsgBox "合格"End IfIf a < 60 ThenMsgBox "不合格"End IfEnd Sub或写成Sub If语句用法()a = Int(Rnd() * 51 + 50)MsgBox aIf a >= 90 ThenMsgBox "优秀"ElseIf 80 <= a And a < 90 ThenMsgBox "良好"ElseIf 60 <= a And a < 80 ThenMsgBox "合格"ElseIf a < 60 ThenMsgBox "不合格"End IfEnd Sub很明显第一种更简结明了。

else也是可以写在同一行的,记得写在同一行的话是不用加end if的,多个条件之间用英文的冒号(:)连接如:Sub if没有endif()a = 2If a = 1 Then MsgBox "不符合": MsgBox "就是不符合": MsgBox "还是不符合" Else MsgBox "符合": MsgBox "找到了"End Sub另外要注意如果把80 <= a And a < 90写成80 <= a < 90意思就不一样了,80 <= a < 90等于80 <= a or a < 90其实if…

else if…

如果分行写ElseIf中间是不用加空格的Sub IF函数用法()Dim 籍贯 As String, 性别 As String, 年龄 As Integer, 面貌 As String,i as IntegerIf Int(Rnd() * 2) = 0 Then 籍贯 = "广东" Else 籍贯 = "广东"If Int(Rnd() * 2) = 0 Then 性别 = "男" Else 性别 = "女"年龄 = Int(Rnd() * 50 + 30)i = Int(Rnd() * 3)If i = 1 Then 面貌 = "党员" Else If i = 2 Then 面貌 = "团员" Else 面貌 = "群众"'请留意这上面一句的用法MsgBox "籍贯:" & 籍贯 & ",性别:" & 性别 & ",年龄:" & 年龄 & ",面貌:" & 面貌If (籍贯 = "广东" And 性别 = "男" And 年龄 > 60) Or ((籍贯 = "广东" And 性别 = "女" And 年龄 > 55) And (面貌 = "党员" Or 面貌 = "团员")) ThenMsgBox "符合条件"ElseMsgBox "不符合条件"End IfEnd Sub例子中有共有四个括号,每个括号代表一个条件,后面的两个括号外面再用另一个括号把两个括号括起来,外面的括号是一个主条件,括号里的条件都是子条件。


以上是文章"

else也是可以写在同一行的,记得写在同一行的话是不用加end i

"的内容,欢迎阅读裹缕时尚健康网的其它文章