View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Nigel[_2_] Nigel[_2_] is offline
external usenet poster
 
Posts: 735
Default Select Case or If - What's the difference

The other major difference is that the first TRUE condition will execute and
the select case code will then exit even if later case conditions are true,
think of select case as nested if statements.

This example will result in the first message box being displayed, the other
two cases are true but will be ignored.
iX = 25
Select Case iX
Case Is = 25: MsgBox "=25"
Case Is 20: MsgBox "20"
Case Is < 30: MsgBox "<30"
End Select

In this example ALL three messages are displayed sequentially
iX = 25
If iX = 25 Then MsgBox "=25"
If iX 20 Then MsgBox "20"
If iX < 30 Then MsgBox "<30"

This nested if sequence replicates the case statement, which is far easier
to understand, above!
iX = 25
If iX = 25 Then
MsgBox "=25"
Else
If iX 20 Then
MsgBox "20"
Else
If iX < 30 Then
MsgBox "<30"
Endif
Endif
Endif


--

Regards,
Nigel




"Risky Dave" wrote in message
...
Hi,

I have a set of about 40 possible Boolean events that I want to examine
and
carry out actions dependant on which are true (more than one may be true
but
I have no way of predicting which ones; it is also possible that none of
them
are true).
I think I can probably do this using either a set of If ...Then
statements:

If statement1=TRUE then
Action 1
End If
If statement 2=TRUE then
Action 2
End If
or I could use a a set of Select Case statements:

Select Case TRUE
Case statement 1
Action 1
Case statement 2
Action 2
End Select

What would be considered the better solution (and why?) or is there a more
efficient way of doing this?

TIA

Dave