Link to home
Start Free TrialLog in
Avatar of brothertruffle880
brothertruffle880Flag for United States of America

asked on

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?

SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

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.
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