+ Reply to Thread
Results 1 to 6 of 6

If, Elseif, Else statements for a UDF

Hybrid View

  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

    I created a UDF as:
    
    Function Num2Cat(rCell As Range)
    
        Dim v As Double, c As Integer
        
        v = rCell.Value
        
        On Error GoTo ErrorHandler
        
        If Not IsNumeric(v) Then Exit Function
            
        If v < -0.1 Then
            c = 0
        ElseIf v >= -0.1 And v < -0.05 Then
            c = 1
        ElseIf v >= -0.05 And v < -0.02 Then
            c = 2
        ElseIf v >= -0.02 And v < -0.005 Then
            c = 3
        ElseIf v >= -0.005 And v < -0.001 Then
            c = 4
        ElseIf v >= -0.001 And v < 0.001 Then
            c = 5
        ElseIf v >= 0.001 And v < 0.005 Then
            c = 6
        ElseIf v >= 0.005 And v < 0.02 Then
            c = 7
        ElseIf v >= 0.02 And v < 0.05 Then
            c = 8
        ElseIf v >= 0.05 And v < 0.1 Then
            c = 9
        Else
            c = 10
        End If
        
        Num2Cat = c
        
    ErrorHandler:
            Exit Function
       
    End Function
    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,858

    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
    Function udftest(rcell As Double) As Double
    Stop 'for debugging and educational purposes remove when you are satisfied the function is working
    lookuparray = Array(-10, -5, -2, -0.5, -0.1) 'fill in the rest, make sure they are in ascending order.
    returnarray = Array(1, 2, 3, 4, 5) 'fill in the rest
    'note that returnarray may not be necessary, as the current example shows the ranking as the position number in lookuparray
    'defining returnarray like this allows for future flexibility if you ever want the rankings to be something other than position in the array.
    lookupposition = Application.WorksheetFunction.Match(rcell, lookuparray, 1)
    'note that match() function return position based on 1 based array. You could assign this value to udftest if you are happy to always return the position number
    'If you use returnarray, note that returnarray is a 0 based array
    'that's why I subtract 1 from lookupposition
    udftest = returnarray(lookupposition - 1)
    End Function
    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
    Function Num3Cat(rCell As Range)
        Select Case rCell
        
        Case Is < -0.1: Num3Cat = 0
        Case Is < -0.05: Num3Cat = 1
        Case Is < -0.02: Num3Cat = 2
        Case Is < -0.005: Num3Cat = 3
        Case Is < -0.001: Num3Cat = 4
        Case Is < 0.001: Num3Cat = 5
        Case Is < 0.005: Num3Cat = 6
        Case Is < 0.02: Num3Cat = 7
        Case Is < 0.05: Num3Cat = 8
        Case Is < 0.1: Num3Cat = 9
        Case Else: Num3Cat = 10
        End Select
    
    End Function
    - 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:

    Function Num2Cat(rCell As Range) As Long 'billj
    
        Dim v As Double, c As Integer
        
        v = rCell.Value
        
        On Error GoTo ErrorHandler
        
        If Not IsNumeric(v) Then Exit Function
        
        Select Case v
        Case Is < -0.1: c = 0
        Case Is < -0.05: c = 1
        Case Is < -0.02: c = 2
        Case Is < -0.005: c = 3
        Case Is < -0.001: c = 4
        Case Is < 0.001: c = 5
        Case Is < 0.005: c = 6
        Case Is < 0.02: c = 7
        Case Is < 0.05: c = 8
        Case Is < 0.1: c = 9
        Case Else: c = 10
        End Select
            
        Num2Cat = c
        
    ErrorHandler:
            Exit Function
       
    End Function
    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