Results 1 to 15 of 15
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    ElseIf Limit (VBA Excel)

    Is there a limit to elseif conditions. Are they considered to be nested?
    TX
    Regards,
    Rudi

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ElseIf Limit (VBA Excel)

    No, an ElseIf part is not nested within the If ... End If.

    I'm not aware of a limit to the number of ElseIf's.

    Consider using a Select Case ... End Select if you have a large number of alternatives.

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ElseIf Limit (VBA Excel)

    Tx. I was just not sure about a limit. I agree about select case.
    As I understand, in ElseIF, each condition needs to be tested whereas with Select Case, it jumps to the appropriate case and does not evaluate each one. This makes select case less system intensive and faster. (If I logically understand) <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    TX
    Regards,
    Rudi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ElseIf Limit (VBA Excel)

    Not, they're not really different in this respect.
    In a series of ElseIf's, each of the conditions is tested in turn until one is satisfied, then the corresponding code is executed and the rest of the ElseIf's is skipped.
    In a Select Case statement, each of the Cases is tested in turn until the correct value is found, then the corresponding code is executed and the rest of the Cases is skipped.

  5. #5
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ElseIf Limit (VBA Excel)

    So the only reason why the Select Case option is then prefered amongst programmers is because it has an easier structure to read and modify??? <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Regards,
    Rudi

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ElseIf Limit (VBA Excel)

    Yes, it looks neater and is easier to edit. The Select Case statement also supports constructs such as

    Case 3, 5, 7 To 12, 14

    which would require lots of And and Or to do in an If / ElseIf statement (or several ElseIfs).

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ElseIf Limit (VBA Excel)

    By the way, all this is standard VB/VBA, it is not specific to Excel.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf Limit (VBA Excel)

    Nope.

    Select Case evaluates each case, in turn, until it succeeeds odor falls thru to the Case Else or End Select.

    Select Case is often easier to maintain and read, but IF is just as good, even better, in particular cases.

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf Limit (VBA Excel)

    We're getting into religion here!

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf Limit (VBA Excel)

    It's standard to ALL programming languages that have both If and SElect statements.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ElseIf Limit (VBA Excel)

    I know, Howard, but I made my remark because Rudi mentioned Excel in the subject.

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf Limit (VBA Excel)

    EXCELent!

  13. #13
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ElseIf Limit (VBA Excel)

    TX guys...

    I suppose I can end of then by saying CASE closed ! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Regards,
    Rudi

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ElseIf Limit (VBA Excel)

    To add my 2 cts: Which is why it is a good idea to do the tests in descending order of probability...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  15. #15
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ElseIf Limit (VBA Excel)

    Agreed....well put!
    Regards,
    Rudi

Posting Permissions

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