+ Reply to Thread
Results 1 to 21 of 21

Nested IF statement with ABS, INT & MROUND

  1. #1
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Nested IF statement with ABS, INT & MROUND

    In a nutshell, I am trying to calculate the difference between 2 cells with 3 decimal places, calculate the absolute value of the result rounding to 2 decimal places to the nearest .05, but have .025 round down and .075 round up. A straight MROUND won't do that, plus I have some negative numbers, which is also challenging for MROUND. In addition, I'd like to use conditional formatting to show the zero and positive results in brown, and the negative results in teal. (This has proven to be two separate challenges, and so far I haven't been able to achieve either result consistently or correctly.)

    CEILING works to round up, and FLOOR works to round down, but the problem is how to isolate which to use in which case. I tried doing a table with just all possible results ending in either .025 or .075 in what should be a reasonable range for the data. I listed them in ascending order. (e.g. 1.025 in column A rounds to 1 in column B, 1.075 in column A rounds to 1.1 in column B, etc.)

    Then I tried to come up with a formula that would look for an exact match, and if no exact match was found, it would then do an MROUND to the nearest .05. I have several variations of possible formulas but in all cases but one I get an #N/A error (value not found). It seems if there is no exact match it doesn't go beyond the table & returns the #N/A error.

    Here are a couple of my draft formulas:

    =IF(VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),MROUND(ABS(C3-C2),0.05))

    =IF(VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),IF((C3-C2)>=0,MROUND((C3-C2),0.05)),-MROUND((C3-C2),0.05))

    -IF(VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),VLOOKUP(ABS(C3-C2),'Table Test'!$A$2:$B$121,2,FALSE),MROUND((C3-C2),SIGN(C3-C2)*0.05))

    The first one returned a result of 5.100 after it was found in the table, but it didn't work on other values that were in the table. None of them got past the table.

    Since I can not possibly list every possible result in a table, is there some other way to do this? I haven't even attempted to do conditional formatting with colors yet, and I fear that yielding an absolute value will make it difficult for the conditional formatting to differentiate the positive from the negative results...

    I have attached a sample spreadsheet along with my table. Note that column G shows the desired result calculated manually, and it refers to column C. I have highlighted both columns for ease of reference.

    Any assistance or insight would be greatly appreciated! Thanks so much!

    Stevie the Kid
    Attached Files Attached Files
    Last edited by DPKologie; 09-01-2012 at 10:14 PM. Reason: To attach sample spreadsheet

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Nested IF statement with ABS VLOOKUP & MROUND with negative numbers if no exact match

    I'm not clear why you would have negative values. If you are taking the absolute value of the difference that will always be positive, where do negative numbers come in?
    Audere est facere

  3. #3
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS VLOOKUP & MROUND with negative numbers if no exact match

    Exactly. MROUND doesn't like dealing with negative numbers, even to get the absolute value of a negative number, and to then round it. I have used a couple of work-arounds as listed above that have worked in the past. The bigger challenge here is getting the formula past the logical test and the VLOOKUP to even get to the MROUND part... and of course to get the VLOOKUP to work correctly as well. (At this point I seem to be getting an #N/A error before I get to the MROUND part, or the "else" option.)

    It could be the syntax, or the formula, or the logic, or my approach, or some combination, but clearly something is flawed. The idea was to first take the absolute value of the difference & look to the table for an exact match, and then if no match is found to take the absolute value and to do a regular MROUND to the nearest .05. Anything not in the table should round properly to the nearest .05. My source numbers are only in .05 increments.

    Perhaps I need to take the absolute value before doing anything further. Any suggestions on how to better do that to get the desired results?

  4. #4
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS VLOOKUP & MROUND with negative numbers if no exact match

    Bump no response.

  5. #5
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS VLOOKUP & MROUND with negative numbers if no exact match

    I had a brain storm about how to isolate the numbers to round up or down by subtracting the integer using INT, rather than use a table or doing a VLOOKUP. I think this will avoid the problem with having to list all possibilities in the table and it seems much cleaner. (I actually thought about starting a new thread but decided to edit the heading instead since it is the same problem but with a different possible solution.)

    I think I am close, but I am getting a #VALUE error and hope perhaps it is just a syntax problem. My logic seems sound... Here are a couple of new draft formulas:

    #=IF(ABS(C3-C2)-ABS(INT(C3-C2))=.025, FLOOR(ABS(C3-C2),0.05)), IF(ABS(C3-C2)-ABS(C3-C2))=.075,CEILING(ABS(C3-C2),0.05)),IF((C3-C2)>=0,MROUND((C3-C2),0.05),-MROUND((C3-C2),.05))#

    #=IF(ABS(C3-C2)-ABS(INT(C3-C2))=.025, FLOOR(ABS(C3-C2),0.05)), IF(ABS(C3-C2)-ABS(C3-C2))=.075,CEILING(ABS(C3-C2),0.05)),MROUND(ABS(C3-C2),0.05))#

    #=IF(ABS(C3-C2)-ABS(INT(C3-C2))=.025, FLOOR(ABS(C3-C2),0.05)), IF(ABS(C3-C2)-ABS(C3-C2))=.075,CEILING(ABS(C3-C2),0.05)),MROUND((C3-C2),SIGN(C3-C2)*0.05)#

    In watching the calculation steps, the #VALUE error is happening between the CEILING and the MROUND steps, and even though it comes up with the correct result, the #VALUE error is superseding it.

    I could really use some help here if anyone has any insights about this. Thank you!

  6. #6
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS VLOOKUP & MROUND with negative numbers if no exact match

    Ok, I think I'm getting closer, but I'm still getting a #VALUE! error. I realized I want anything ending in .x25 needs to be rounded down, but anything ending in .x75 should already be rounded up with MROUND, so I don't think I need to use CEILING.

    Here's my latest draft formula:

    Please Login or Register  to view this content.
    I think if I can get the syntax right this seems like it should work? In doing a formula evaluation and watching the calculations steps it is working fine until it gets to FLOOR, then I get a #N/A, which is resulting in the overall #VALUE! error. In my example all the OR statements were false so it should have skipped over FLOOR and gone right to MROUND.

    Does anybody know why FLOOR is giving me #N/A in this formula? Seriously, I feel totally stuck.
    Last edited by DPKologie; 09-02-2012 at 02:45 AM.

  7. #7
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Nested IF statement with ABS, INT & MROUND

    Please Login or Register  to view this content.
    I think people forget the word "THANK YOU!!!!" Do you still know it???

    There is a little star ( ADD REPUTATION ) below those person who helped you. Click it to say your "PRIVATE APPRECIATION TO THEIR EFFORT ON THEIR CONTRIBUTIONS "

    Regards,
    Vladimir

  8. #8
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Nested IF statement with ABS, INT & MROUND

    If you are after the values in column H-Momentum 1

    =IFERROR(IF((MID(ABS(C3-C2),4,2)*1)<7,FLOOR(ABS(C3-C2),0.05),MROUND(ABS(C3-C2),0.1)),MROUND(ABS(C3-C2),0.1))

  9. #9
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Nested IF statement with ABS, INT & MROUND

    there is a lapses on the above..
    here's better i think
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS, INT & MROUND

    Vlady, you are very creative and this looks promising! Your first formula gave me a .7 instead of .65 for G3, but that was fixed and returned .65 with your most recent formula.

    Unfortunately, your new formula returned .4 for G17 instead of .350 as in H17, and it returned 1.4 instead of 1.45 in G22 as in H22. (I need to round to 2 decimal places, not 1, and to the nearest .05 with .x25 rounding down, and .x75 rounding up.)

    I have never used IFERROR or MID, so I am not familiar with how they work. If you could enlighten me, that would be fantastic. Perhaps it will work with a little tweaking? It looks like it traps any errors and extracts text? Does it also extract numbers? The syntax is confusing for me but it looks like an ingenious way to isolate the decimals rather than using INT. Very clever!

    Maybe somehow having it <75 and using FLOOR? Again, I'm not familiar enough with these functions to know how the syntax should be, and I'm not clear why MROUND is listed twice and at .1 instead of .05?

    I would also like to know why my formula is getting a #VALUE error, since I don't see any text in my referenced cells in column C. I wondered if my data links were a problem, but after replacing those with values I still had the error. Maybe we could even combine our formulas somehow? Do you know what is going on there, or can anybody shed some light on that?

    Thanks so much for your help, vlady!

    Any help or suggestions anyone has for me here would be most welcome!

  11. #11
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested IF statement with ABS, INT & MROUND

    See if this works

    =MROUND(ABS((C3-C2)-((--TEXT(MOD((C3-C2),0.1),"0.000")=0.025)*0.01)),0.05)

    Or to keep the result negative (where applicable)

    =MROUND(ABS((C3-C2)-((--TEXT(MOD((C3-C2),0.1),"0.000")=0.025)*0.01)),0.05)*SIGN(C3-C2)

  12. #12
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Nested IF statement with ABS, INT & MROUND

    i got it also for Cell G29

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested IF statement with ABS, INT & MROUND

    Thanks for pointing that out vlady, I didn't see those differences.

    This works with the given samples and similar data, i.e. all values are multiples of 0.025

    =MROUND(ABS(C3-C2)+SIGN(MOD(ABS(C3-C2),0.1)-0.05)*0.01,0.05)

  14. #14
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS, INT & MROUND

    Wow, thanks to vlady and jason.b75 for your suggestions and input. Both of these recent formulas looked promising, creative and clever; however unfortunately neither of them returned the correct results in all cases. And again, I am not familiar enough with these functions to know how to modify them.

    I have attached a sample test worksheet comparing both formulas to the desired results with examples. Jason.b75's formula had trouble rounding .x75's up and I got a #NUM error when the result should be zero. And vlady's formula had trouble when the difference was already only 2 decimal places, rounding up in some cases and down in others when the difference ended in .x50.

    Perhaps these formulas could be modified to correct this? If anybody can help with this, that would be great! Thanks so much!
    Attached Files Attached Files

  15. #15
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,361

    Re: Nested IF statement with ABS, INT & MROUND

    The above formula of jason works. post #13

  16. #16
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested IF statement with ABS, INT & MROUND

    Just in case there was a problem with the formula in post #13 that we both missed, here's another alternative.

    =MROUND(ABS(C3-C2),LOOKUP(ROUND(MOD(ABS(C3-C2),0.1),3),{0,0.0251,0.075},{0.1,0.05,0.1}))

  17. #17
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS, INT & MROUND

    Quote Originally Posted by vlady View Post
    The above formula of jason works. post #13
    Unfortunately, although I was initially very hopeful, as I did further testing on additional actual data, jason's formula from post #13 didn't work in all cases.

    See my above attachment in post #14, Sheet1, cells G4, G8, G13, G24, G28, G32, G34, G35, G39, G47, G50, G52, and G55 for examples of cases where jason's formula didn't produce the correct result. I have listed the actual absolute difference in column F, and the desired result in column H on those rows. Unfortunately in those cases, differences ending in .x75 rounded down instead of up, and there was a #NUM error when the difference was zero. His formula seemed to handle rounding .x25 differences down just fine though.

    As you can see, the challenge is to both round the .025's down and round the .x75's up, as well as be able to handle a zero difference, and to not round .x50's at all, since they are already at 2 decimals.

    Having said that, I want to again put out the challenge as to how to accomplish this in all cases. And I want to again thank you and jason for your work on this puzzling problem. You are both coming up with things that are beyond my experience. I am hopeful that a little tweaking may do the trick...?

    I would so value any additional help on this!

  18. #18
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested IF statement with ABS, INT & MROUND

    The formula from #13 works fine, you changed the divisor of the MOD() function from 0.1 to 0.01 which is the reason for the incorrect results.

    The only point it does fail, as you noticed, is with a zero difference, which this will correct.

    =MROUND(ABS(ABS(C3-C2)+SIGN(MOD(ABS(C3-C2),0.1)-0.05)*0.01),0.05)

    This, and the alternative (and, in my opinion, better) formula I suggested in post #16 both return the correct results.

  19. #19
    Registered User
    Join Date
    03-23-2012
    Location
    Malibu, California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Nested IF statement with ABS, INT & MROUND

    Jason, I didn't realize I had mistyped your formula from #13, which changed the results. Thank you for pointing that out! I have been testing your newer formula from #16, and so far it is working perfectly! You are so amazingly awesome, and I am thrilled beyond belief!

    I am curious why you think that your formula from #16 is better than the one from #13? Also, could you please explain to me how they work? I would be so grateful to be able to understand them for future reference!

    And I am also wondering if you or anyone knows why my convoluted formula from #6 returned a #VALUE error? I'd like to know why it didn't work as well and if my basic logic was sound.

    In any case, I plan to incorporate your formula from #16 (the one you thought was the better of your formulas) into my model. It is so much cleaner than any of my attempts, and it seems to be working flawlessly. Kudos, mate!

  20. #20
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested IF statement with ABS, INT & MROUND

    Starting with why I think the formula from #16 is the better of the 2, it's less convoluted and has a higher degree of accuracy. Although the error is not noticeable on this data sample, the adjustments used to 'correct' the rounding would return incorrect results with other values, for example a value of .x33 would incorrectly round to .x00 instead of the expected .x50. Whilst the formula could be changed to rectify this, the formula from #16 doesn't use adjusted values so will not encounter the same problem. Also it's easier to change for different criteria.

    Next, with your formula,

    Please Login or Register  to view this content.
    You had 1 small error, an extra closing paranthesis at the end of the section I've marked in red, which should have been at the end of the formula. The corrected version above should work, but you might still have some errors with .x25 values.

    With certain types of calculation, there is a small degree of decimal inaccuracy, for example, subtracting a negative from another negative, a result of 0.025 would be returned as something like 0.02499999999999998, as you can see the difference is almost insignificant, but it is still a difference, so your formula wouldn't find the match.

    To try and exlpain my formula,

    =MROUND(ABS(C3-C2),LOOKUP(ROUND(MOD(ABS(C3-C2),0.1),3),{0,0.0251,0.075},{0.1,0.05,0.1}))

    You already know how the ABS function works,
    MOD(ABS(),0.1) gives the remainder of the ABS() result when divided by the defined value, in this case, 0.1, i.e. 0.0xx, now we only have to compare 2 digits, not 3, as an example, MOD(ABS(C17-C16),1) is the same as ABS(C17-C16)-ABS(INT(C17-C16)).

    ROUND(MOD(ABS(...),0.1),3) rounds the current result value to 3 decimal places, this corrects any inaccuracy (0.024999999..) to elimintate possible errors.

    See http://support.microsoft.com/kb/78113

    At this point, the formula will hold a value of 0.000, 0.025, 0.050 or 0.075, lookup looks at the array {0,0.0251,0.075} for the highest value that is less than or equal, then uses the corresponding result from the second array as the factor for MROUND

    0.00 to 0.025, lookup returns 0, corresponding value = 0.1 (MROUND(value,0.1)
    0.0251 to 0.0749999, lookup returns 0.0251, corresponding value = 0.05 (MROUND(value,0.05)
    0.075 to 0.099999, lookup returns 0.075, corresponding value = 0.1 (MROUND(value,0.1)

    Hopefully that all makes sense.

    In case it got missed before, I did mention in an earlier post that if required, there is a simple way to retain the result as negative where applicable,

    =MROUND(ABS(C3-C2),LOOKUP(ROUND(MOD(ABS(C3-C2),0.1),3),{0,0.0251,0.075},{0.1,0.05,0.1}))*SIGN(C3-C2)

    SIGN() simply returns 1 0 or -1 for positive zero and negative values respectively, multiplying the ABS() result by the sign of the original result is a simple, yet effective way to convert rounded values back to negative.

  21. #21
    Registered User
    Join Date
    01-17-2013
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Nested IF statement with ABS, INT & MROUND

    Jason.b75 - you sir are a genius.

    You've just solved an issue I was having with data rounding up when it should of rounded down.

    =MROUND(ABS(C3-C2),LOOKUP(ROUND(MOD(ABS(C3-C2),0.1),3),{0,0.0251,0.075},{0.1,0.05,0.1}))

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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