VBA Excel Question: If then Else VERSUS select case

Are the "If then else" and "Select Case" interchangable?  It seems that they can both be used in the same applications.  Or are one of them more appropriate/efficient/etc.  at some tasks?

brothertruffle880Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
In a simple either/or situation  "If Then Else" is probably bes, but if can have multiple outcomes, then Select case is probably better.

It is especially powerful because it can accept multiple values in a list and effectively 'OR' them

Select case MyInteger
    Case 1 to 5, 8 to 9
         ...
    Case 12 to 17
          ...
    Case 29, 31, 33
        ...
    case else
        ...
End select
         
0
GrahamSkanRetiredCommented:
You can create a structure similar to Select case with If and ElseIf. This is good for testing different items

If a=1 then
   ...
Elseif b=2 then
   ...
Elseif c = "Some text" then
  ...
End if

 
0
GrahamSkanRetiredCommented:
Corrected first sentence of my penultimate comment (37269054)

In a simple either/or situation  "If Then Else" is probably best, but if you need multiple outcomes, then Select Case is probably better.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
They both have advantages - Select Case is easier to read and manage.

If (ElseIf) End If can work with multiple values and use logical expressions/operators, but can be difficult to follow.
0
Martin LissOlder than dirtCommented:
Select Case can be a lot easier to write. For example

If x = 1 or x = 5 or x = 12 or x > 20 Then…
    'do something

Open in new window

compared to
Select Case x
    Case 1, 5, 12, > 20
        'do something
    

Open in new window



-------------------------------------------------------------------------------------------------------------------------------
My Articles:
Using the VB6 DebuggerAutomatic Insertion of Procedure Names
A Textbox ActiveX Control That Limits Input to NumbersSpell Check a Textbox
Improved Formatting TagsConditional Compilation

Marty - MVP 2009, 2010, 2011
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.