13

一个非常简单的问题:考虑If...Then...Else到 VBA 中的一条指令,之后如何分隔多条指令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"),

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

4

2 回答 2

23

多个语句将由一个新行分隔:

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
于 2013-03-17T10:55:31.383 回答
1

这适用于多个语句:

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
于 2015-04-30T20:08:42.263 回答