如果......那么......之后有多个语句

时间:2013-03-17 10:09:04

标签: vba if-statement conditional-statements

一个非常简单的问题:考虑VBA中的If...Then...Else指令,如何在Then之后分隔多条指令?换句话说,我应该写一些类似

的东西
If condition [ Then ]    
   [ statement1 ] & [statement2] 
Else [Else statement] (i.e. using "&"),

If condition [ Then ]         
   [ statement1 ] And [statement2] 
Else [Else statement] (i.e. using "And"),

或其他一些分隔符/命令?

2 个答案:

答案 0 :(得分:19)

多个陈述用新行分隔:

If SkyIsBlue Then
  StartEngines
  Pollute
ElseIf SkyIsRed Then
  StopAttack
  Vent
ElseIf SkyIsYellow Then
  If Sunset Then
    Sleep
  ElseIf Sunrise or IsMorning Then
    Smoke
    GetCoffee
  Else
    Error
  End If
Else
  Joke
  Laugh
End If

答案 1 :(得分:-2)

这适用于多个陈述:

if condition1 Then stmt1:stmt2 Else if condition2 Then stmt3:stmt4 Else stmt5:stmt6

或者你可以将它分成多行:

if condition1 Then stmt1:stmt2
Else if condition2 Then stmt3:stmt4
Else stmt5:stmt6