Discussion Locked

VBA or VB - If /Select statement

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

6 replies

by johnfriz

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.

by rrv

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

by bbg

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.

This discussion is closed to new replies.