+ Reply to Thread
Page 8 of 12 FirstFirst ... 678910 ... LastLast
Results 106 to 120 of 167

Thread: What to Avoid in VBA

  1. #106
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: What to Avoid in VBA

    Quote Originally Posted by pierre08
    Then why using Select Case in better Than If..End If???
    This was covered vaguely from around post #72 ... ie efficiency versus practicality.

  2. #107
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: What to Avoid in VBA

    The point is somewhat academic unless you've got the Select statement inside a loop executing a zillion times. It will take the next programmer more time to figure out what efficient but weird code does than time saved in execution for the user.
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  3. #108
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: What to Avoid in VBA

    Quote Originally Posted by Paul View Post
    Hearing that If...ElseIf...Else is more efficient than Select Case is interesting, as every one of my programming teachers in college said the exact opposite. They're reasoning was that Select Case structures only check for the variable's value once and then perform each of the cases in order, whereas an If/ElseIf has to test it for every "case" as it were. If you have a Select Case with 20 cases and one variable test at the beginning vs If..ElseIf..Else with those same 20 tests done in order, one (ok, me) would think the Select Case is faster.

    Have I been led astray?!?
    Since it came up again I thought I'd re-ask (since I didn't get a response)... why would all my instructors tell me what I've stated above? Are they stupid, hence making me stupid through having listened to them?

  4. #109
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: What to Avoid in VBA

    Not sure about the instructors, probably just teaching what they've been taught.

    But when I'm using F8 to step through my code, Select Case and If...ElseIf...Else...End If both do the same thing...they go through the options in order listed in the code and as soon as a TRUE evaluation occurs...it skips out thus not testing any IFs or CASEs that followed.

    So I wonder where the increased efficiency for one over the other even comes from?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #110
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: What to Avoid in VBA

    I always assumed the the difference was that "Case" exits when the first true statment is found
    and If...ElseIf...Else... evaluates all the statments ..
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  6. #111
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: What to Avoid in VBA

    No, I just ran this and it skipped to the end when it found "2" in [A1]
    Sub test()
    
        If Range("A1") = 1 Then
            Range("B1") = 2
        ElseIf Range("A1") = 2 Then
            Range("B1") = 3                      'stopped here
        ElseIf Range("A1") = 3 Then
            Range("B1") = 4
        ElseIf Range("A1") = 4 Then
            Range("B1") = 5
        End If
    
    End Sub
    
    
    Sub test()
    Select Case Range("A1").Value
        Case 1
            Range("B1") = 2
        Case 2
            Range("B1") = 3         'stopped here
        Case 3
            Range("B1") = 4
        Case 4
            Range("B1") = 5
        Case 5
            Range("B1") = 6
    End Select
    
    End Sub
    Last edited by JBeaucaire; 02-26-2010 at 04:33 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #112
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: What to Avoid in VBA

    I always assumed ...
    Both evaluate only to the first True condition. You know that, you just didn't stop to think it through.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #113
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: What to Avoid in VBA

    Hard to say without seeing the generated code, but in both statements you have to evaluate dynamic expressions. Hard to say why either one would have a significant advantage. I suspect if there's a difference it would be somewhat academic. The IF statement require it to evaluate one expression per branch, but the Select requires it to evaluate one expression on the Select line, plus one expression per branch, so it has to do a little more evaluation.

    (You compile VBA so I assume it must generate code, though I have this nagging feeling it's interpreted.)
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  9. #114
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    768

    Re: What to Avoid in VBA

    A nice simple illustration here. If you step through it you will notice that the function myF() gets called twice in the "If ... elseif" and only once in the case statement. Which must mean more work is being done in the "if ... elseif". This will be compounded by more complex conditions and increasing numbers of elseif.

    But of potentially greater concern is that the value of test maybe different at each elseif. For example a time based test will have a different value for the comparison at each point. Whereas the "case" statement will only use one value for comparison.

    Additionally there is always the possibility that the elseif is not testing exactly the same condition a myF1("0") and myF1("O") may easily be overlooked. Using a select case ensures that only the one value is being used as the source.

    Sub main()
    
    Dim sI As String
    Dim sC As String
    
    
        Do
            Range("a1") = "B"
            If myF() = "A" Then
                sI = "if 1  "
            ElseIf myF() = "B" Then
                sI = "if 2  "
            Else
                sI = "IF is not matched    "
            End If
            
            Range("a1") = "B"
            Select Case myF()
            Case "A"
                sC = "case 1"
            Case "B"
                sC = "case 2"
            Case Else
                sC = "case not matched"
            End Select
        Loop While MsgBox(sI & sC, vbOKCancel) = vbOK
    
    End Sub
    
    Function myF() As String
        myF = Range("a1")
        Range("a1") = InputBox("enter value")
    End Function


    Hope this was useful or entertaining.

  10. #115
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    768

    Re: What to Avoid in VBA

    just in case (excuse the pun) it needs saying: it is also obvious that the conditions in the "case" statements could give rise to further dynamic conditions. These should, in my view, be avoided.

    My main point was to illustrate that the myF function gets evaluated on each line of the "IF" but only once in the "case"


    Hope this was useful or entertaining.

  11. #116
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: What to Avoid in VBA

    A slightly contrived example, IMO, since in reality you would store the value in a variable at the start then compare the variable in the If statements, just as in the Select Case, unless you actually had a need to re-evaluate it each time.
    Note: in languages like C++, I believe that switch statements are more efficient due to the way the compiler optimises them.
    Last edited by romperstomper; 02-26-2010 at 06:43 PM.

  12. #117
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,802

    Re: What to Avoid in VBA

    don't eat yellow snow !
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  13. #118
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: What to Avoid in VBA

    A slightly contrived example, IMO ...
    And if the return value of myF() could vary within the same code block (e.g., Rnd()), it would be a very wacky piece of code.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  14. #119
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    768

    Re: What to Avoid in VBA

    Quote Originally Posted by shg View Post
    And if the return value of myF() could vary within the same code block (e.g., Rnd()), it would be a very wacky piece of code.
    I have seen it happen

    Romperstomper : you are right I would store the value in a variable, but not everyone else does.


    Hope this was useful or entertaining.

  15. #120
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: What to Avoid in VBA

    That's true, but it's not really a demonstration of the performance of Select Case versus If... Else.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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