+ Reply to Thread
Results 1 to 5 of 5

Problem converting a very simple nested IF function to a UDF

  1. #1
    Registered User
    Join Date
    06-12-2014
    Posts
    8

    Problem converting a very simple nested IF function to a UDF

    Hello Excel Gurus,

    I haven’t been here for a while (my loss), and I am again in need of enlightenment.

    I have the following nested IF function (below) that I’m trying to convert to a UDF, because the actual cell references to be used are long INDEX functions that make the IF function cumbersome to read and follow.

    =IF(D$6=0,"NA",IF(D$6<0,((D$5/D$6-1)*-1),D$5/D$6-1))

    The function is simply to calculate change, while avoiding #DIV/0! errors (returning an “NA” instead), and reversing the sign when the denominator is a negative number.

    The VBA code I’ve been attempting to use is below, where the numerator is News (D$5 – above) and the denominator is Prev (D$6 – above):

    Please Login or Register  to view this content.
    The problem is that the code is skipping the "Else" or not recognizing the "If Prev < 0 Then" statement so when Prev is negative the resulting evaluation does not get multiplied by * -1, as it otherwise would if I were using the straight IF function from above.

    I’ve scoured the forum and Internet for a clue as to what I’m doing wrong, but can’t find the solution. The worst part is that code is so simple the answer must be right below my nose.

    Any help is greatly appreciated! Thanks,

    Gabriel G

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,933

    Re: Problem converting a very simple nested IF function to a UDF

    Hi, welcome to the forum

    Not sure if this will help with the VBA, but it looks like that formula can be shortened to this...
    =IF(D$6=0,"NA",ABS((D$5/D$6-1)))
    (test it to make sure)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Problem converting a very simple nested IF function to a UDF

    You have an If Zero condtion that results in NA
    But your ELSE condition checks for negative vals and calculates; however upon exiting the If<0 block you recalculate with:
    CHANGE = (News / Prev - 1)
    So, Change is always NA for zero or (News / Prev - 1) for not zero.
    Please Login or Register  to view this content.
    Last edited by protonLeah; 07-22-2015 at 11:18 PM.
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    06-12-2014
    Posts
    8

    Re: Problem converting a very simple nested IF function to a UDF

    Hi protonLeah,

    Thanks for your solution.

    The way you describe the problem with my original code makes me think that I was conceptualizing the code in terms of how the straight IF function works, but not how IF statements work in VBA.

    Your solution is much simpler, and makes much better use of VBA programming. I have seen Case before, but didn't really know how to apply it in this instance.

    Thanks again for you help!

    Regards,

    GabrielG

  5. #5
    Registered User
    Join Date
    06-12-2014
    Posts
    8

    Re: Problem converting a very simple nested IF function to a UDF

    Hi Ford,

    Thanks for you help and suggestion. In this instance the ABS wouldn't work as I planned, because it would still give me a positive value when say, the denominator is +10, and the numerator is +20, which should give me a -100% results.

    All the same, thanks for looking into my problem.

    Regards,

    GabrielG
    Last edited by GabrielG; 07-23-2015 at 09:58 AM.

+ 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. Problem with Nested IF Function?
    By anasmiff in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-12-2014, 10:25 AM
  2. [SOLVED] Converting nested IFs into VBA function
    By dtipitino in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-21-2014, 10:19 AM
  3. [SOLVED] simple search function using nested IF and Vlookup statements
    By cherryt in forum Excel Formulas & Functions
    Replies: 21
    Last Post: 08-27-2013, 08:45 AM
  4. [SOLVED] Problem with using istext in a nested IF function
    By Ciarmccabe in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-09-2013, 06:30 PM
  5. [SOLVED] Converting a simple Excel function to a Macro.
    By Jongering in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-12-2012, 08:21 AM
  6. nested if function problem
    By headache in forum Excel General
    Replies: 7
    Last Post: 01-10-2007, 06:05 PM
  7. Problem with nested if function. PLS HELP
    By sfar007 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-06-2006, 06:34 AM
  8. Problem using SUM() function with nested IF()
    By JWeb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-21-2005, 04:04 PM

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.6.0 RC 1