Select Case or If - What's the difference

R

Risky Dave

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
 
J

Jacob Skaria

Thinking of a better solution would be to keep your boolean events as an
Array so that you can loop through the array which saves lines of code..

If this post helps click Yes
 
N

Nigel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top