+ Reply to Thread
Results 1 to 6 of 6

If, Elseif, Else statements for a UDF

  1. #1
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    If, Elseif, Else statements for a UDF

    Hello everyone,

    I wanted to converted numeric values in a column to rating scales from 0 - 10. The scheme is:

    ER < -10.0% 0
    -10.0% <= ER < -5.0% 1
    -5.0% <= ER < -2.0% 2
    -2.0% <= ER < -0.5% 3
    -0.5% <= ER < -0.1% 4
    -0.1% <= ER < 0.1% 5
    0.1% <= ER < 0.5% 6
    0.5% <= ER < 2.0% 7
    2.0% <= ER < 5.0% 8
    5.0% <= ER < 10.0% 9
    ER >= 10.0% 10

    Please Login or Register  to view this content.
    But is there a better way to define this UDF for my purpose? I feel the If, Elseif, Else statements are not that tidy at all.

    Many thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: If, Elseif, Else statements for a UDF

    I'd go with Select Case rather than if, eleseif... always strikes me as easier to read or maintain, but I can't say it would make much difference in performance, given that the options are not that many. An If statement (as far as I know) gets fully evaluated, whereas the Case method would stop evaluating once it reaches the "true" condition.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: If, Elseif, Else statements for a UDF

    Your example shows a basic "lookup table" kind of thing, so I might set up a lookup table kind of thing in VBA.
    1) Assign array of lookup values (rank divisions) to an array.
    2) Use one of Excel's lookup functions (I would use Application.WorksheetFunction.Match()) to find the position of rcell in the array.
    3) (optional) assign an array of return values to an array (if you ever want to use something other than position in array for the ranking).
    4) Use the result of 2 to return the appropriate value from the array created in 3.

    A quick and incomplete function I put together
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: If, Elseif, Else statements for a UDF

    Try
    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: If, Elseif, Else statements for a UDF

    Another:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  6. #6
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: If, Elseif, Else statements for a UDF

    Many thanks, everyone. They are much better codes than my basic IF ... ELSE ones. They are really useful to me. Cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] When ElseIf condition is met, the statements under elseif are not happening.
    By excelkann in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-07-2016, 12:40 PM
  2. Code stepping in to elseif statement when elseif is definitely not true
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2013, 05:31 PM
  3. [SOLVED] If Else and ElseIf statements
    By paulary30 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-10-2013, 04:44 PM
  4. [SOLVED] VBA: Use Array instead of using ElseIf statements
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2012, 12:44 PM
  5. If Elseif elseif problem
    By tjbillund in forum Excel General
    Replies: 3
    Last Post: 08-10-2011, 07:02 AM
  6. Ordering If, Else, and ElseIf Statements in VBA
    By BrownTeddyBear in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2009, 01:51 AM
  7. [SOLVED] Re: IF..Then..ELSE.. ELSEIF
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM

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