Is there any difference between these 2 codes:
![]()
Please Login or Register to view this content.
Assuming there is no programmic difference, is there a performance difference?![]()
Please Login or Register to view this content.
Is there any difference between these 2 codes:
![]()
Please Login or Register to view this content.
Assuming there is no programmic difference, is there a performance difference?![]()
Please Login or Register to view this content.
There is no logical difference but select case is better and more efficient. This is because each IF statement must be executed one at a time, but the Select Case statement is only executed once and then it moves on to the next stage in the code
Are you saying that if "a = b" in my example, that "v = 8" and "d <> 7" are still evaluated?
Your select case statement is incorrect
Select case tests 1 item against muliple results
If and Ifelse statements are designed for testing different items for different results as you have used in your original posting![]()
Please Login or Register to view this content.
Please Read Forum Rules Before Posting
Wrap VBA code by selecting the code and clicking the # icon or Read This
How To Cross Post politely
Top Excel links for beginners to Experts
If you are pleased with a member's answer then use the Scales icon to rate it
If my reply has assistedor failed to assist you
I welcome your Feedback.
foxguy
There's one major difference between them - it's far easier to work with Select Case rather than If...ElseIf...End If.
mudraker
How is the original Select Case incorrect, it's just kind of 'backwards'?
I've seen that syntax, ie testing against True, used before, might even have tried it myself but I couldn't quite get my head around it.![]()
well hush my keyboard
I should have done some testing 1st on the Select Case True code before replying
The only thing wrong with it turned out to be Select Case at the end instead of End Select.
This is the 1st time I seen Select Case used this way.
The reason I asked:
When I'm debugging and stepping through 1 line at a time, I can edit the "ElseIf" lines without having to reset. If I try to change/add a "Case" line, I get the warning that it will reset everything. Sometimes I'll have a bug in the "Case ..." line, and there's no way to resolve it except to reset. That never happens with the "ElseIf" lines.
I do agree that using the "Select" is easier to work with and easier to decipher, etc., but it's a pain sometimes when debugging.
Mudraker, I thought "Select Case True" was fairly common. Is there some reason I shouldn't use it?
A Google search showed a few examples of codes using Select Case True, most example I saw refered to check a cell or variable type value
e.g
![]()
Please Login or Register to view this content.
As Select Case True works I see no reason not to use it apart from the problem of having to halt/crash the macro when editing any of the Case statements.
As for me not using it is simpy a case of being mainly self taught and not having come across a Case Statement using Select Case True or if I did, I did not fully understand it.
Noe that I am aware of it I probally will be using it myself
foxguy
If the problem with debugging is as you describe I don't think it's to do with what syntax or structure you are using - it's just 'one' of those things and a mild irritation.
If I was to record the times when the IDE resets when not expected and vice versa it'd take a few reams.
mudraker
Didn't notice the 'Select Case' at the end - honest.:oops:
I've tried using Select Case True in the past but soon gave up after my small brain couldn't quite get the logic.![]()
foxguy
With If & Elseif statements each statement is tested until a true result is achieved, the rest of the If statements are not testedAre you saying that if "a = b" in my example, that "v = 8" and "d <> 7" are still evaluated?
With Case statements I could not find anything that says if all statements are checked or each 1 is checked until a match is found
The only reference I could find was in the Help file
. which hints at each statement is tested until a true result is acheivedIf expression in more than one Case clause match, only the statements following the first match are executed
Norie
As per your earlier post itWith all the Select case statements I'm familiar with you have a variable, cell value etc that you test multiple results against. - a match gives a true resultit's just kind of 'backwards'?
With using Select Case True you are testing various conditions to you get one that gives a true result - This you are not limited to just testing against 1 cell value or variable
mudraker
I think you'll find that's wrong.
Once a case is matched then the this is what happens.
Like this I think.If testexpression matches any Case expressionlist clause, the statements following that Case statement run up to the next Case, Case Else, or End Select statement. Control then passes to the statement following End Select.
I got this here.![]()
Please Login or Register to view this content.
Norie
What part of my previous reply do you think is wrong?
mudraker
Actually I think I was being a little hasty there - I had just started reading through what I'd found on the Web.
So I probably misinterpreted what I was reading and what you were saying - making me 'wrong' I suppose.
When reading further it seems there isn't much difference between Select Case and If ElseIf EndIf.
Mnd you I'm sure I've been involved in discussions on the topic before that went as far as to performance issues.
When people start talking milliseconds I tend to get my coat.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks