+ Reply to Thread
Results 1 to 13 of 13

is there a difference between Select Case and If..Then

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    is there a difference between Select Case and If..Then

    Is there any difference between these 2 codes:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Assuming there is no programmic difference, is there a performance difference?

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: is there a difference between Select Case and If..Then

    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

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: is there a difference between Select Case and If..Then

    Are you saying that if "a = b" in my example, that "v = 8" and "d <> 7" are still evaluated?

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: is there a difference between Select Case and If..Then

    Your select case statement is incorrect

    Select case tests 1 item against muliple results
    Please Login or Register  to view this content.
    If and Ifelse statements are designed for testing different items for different results as you have used in your original posting
    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 assisted or failed to assist you I welcome your Feedback.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: is there a difference between Select Case and If..Then

    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.

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: is there a difference between Select Case and If..Then

    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.

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: is there a difference between Select Case and If..Then

    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?

  8. #8
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: is there a difference between Select Case and If..Then

    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

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: is there a difference between Select Case and If..Then

    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.

  10. #10
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: is there a difference between Select Case and If..Then

    foxguy

    Are you saying that if "a = b" in my example, that "v = 8" and "d <> 7" are still evaluated?
    With If & Elseif statements each statement is tested until a true result is achieved, the rest of the If statements are not tested

    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
    If expression in more than one Case clause match, only the statements following the first match are executed
    . which hints at each statement is tested until a true result is acheived

    Norie
    As per your earlier post it
    it's just kind of 'backwards'?
    With 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 result

    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

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: is there a difference between Select Case and If..Then

    mudraker

    I think you'll find that's wrong.

    Once a case is matched then the this is what happens.
    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.
    Like this I think.

    Please Login or Register  to view this content.
    I got this here.

  12. #12
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: is there a difference between Select Case and If..Then

    Norie

    What part of my previous reply do you think is wrong?

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: is there a difference between Select Case and If..Then

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1