Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If...Then...Else with multiple statements after Then

a very easy question: considering an If...Then...Else instruction in VBA, how can I separate multiple instructions after Then? In other words, should I write something like

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

or

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

or some other separator/command?

like image 954
Avitus Avatar asked Mar 17 '13 10:03

Avitus


2 Answers

This works with multiple statements:

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

Or you can split it over multiple lines:

if condition1 Then stmt1:stmt2
Else if condition2 Then stmt3:stmt4
Else stmt5:stmt6
like image 82
user4852244 Avatar answered Oct 04 '22 01:10

user4852244


Multiple statements are to be separated by a new line:

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
like image 30
NGLN Avatar answered Oct 04 '22 01:10

NGLN