General discussion

Locked

VBA or VB - If /Select statement

By BBG ·
Which is faster - using an IF/THEN statement or a SELECT statement?
For a single conditional test?
For multiple conditions?

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

VBA or VB - If /Select statement

by johnfriz In reply to VBA or VB - If /Select st ...

Hi,

For a single condition test IF/THEN would be faster because it evaluates the expression and tests whether its true in one go.

However, for multiple conditions (more than 2:- for a 2 condition test use IF/THEN ELSE), SELECT would be faster.This is because it only has to evaluate the expression once and then test against each case statement. Also, SELCET is more modular and readable if you have a good few conditions.

Regards,
John.

Collapse -

VBA or VB - If /Select statement

by BBG In reply to VBA or VB - If /Select st ...

Poster rated this answer

Collapse -

VBA or VB - If /Select statement

by RRV In reply to VBA or VB - If /Select st ...

In VB IIF will the simple condition. For more than one but less than 3 conditions if/then. More than 3 conditions, Select statement. If conditions require string matching, If condtion with STRCOMP function would be the best.
RV

Collapse -

VBA or VB - If /Select statement

by BBG In reply to VBA or VB - If /Select st ...

Poster rated this answer

Collapse -

VBA or VB - If /Select statement

by BBG In reply to VBA or VB - If /Select st ...

I am not going to rate yet because I am still trying to find the time to effectively test.
But, the question is concerning speed and not easy reading. Also, a IIF function will often not work because of the amount of code, and complexity, that may be needed.

As far as some testing goes, I have determined that if an If/ElseIf is used with the same comparison being checked in each branch, then it is faster to assign the comparison/condition to a variable (like using: bIsTrue = myFunction(TestMe), and then testing the Boolean variable in the If/ElseIf rather than calling the same condition comparision at each branch.

I am finding that if there is more than one condition check (If/ElseIf) then the Select Case is almost always as fast as the If/Then, and, for one condition test, they are often just as fast as one another.

Collapse -

VBA or VB - If /Select statement

by BBG In reply to VBA or VB - If /Select st ...

This question was closed by the author

Back to Web Development Forum
6 total posts (Page 1 of 1)  

Related Forums