+ Reply to Thread
Results 1 to 36 of 36

number closest to 0

  1. #1
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    number closest to 0

    Hi there,

    I'm a medical student currently working on a paper for which I'm doing data analysis of patients.
    I ran into issue for which I could find no answer on the internet, nor anyone I knew who works with Excel could help me. Hopefully someone here can.


    1. Which formule is there to select, in each row, the number closest to zero, so that the cell next to it will get a "1". As depicted in the following picture:
    pic1.png

    2. To add to that, my aim is to have this formula to select the number closest to zero, for each individual patient. Like in the picture below (no real names used)
    pic2.png

    3. Finally, I only want the number closest to zero to be selected if it is within a margin of -90 to 90. For example, if one patient has 95 as 'number closest to zero' I don't want it be selected.

    I'm really hoping there is a formula to do all this, otherwise I will have to go through hunderds of patients to select all these numbers by hand.

    thanks in advance

    EDIT:
    I missed the rule about crossposting, my apologies. I posted my questions here as well: https://www.mrexcel.com/forum/excel-...ml#post5087891

    However, I prefer to continue in this post for this and following issues, if that would be alright.
    Attached Files Attached Files
    Last edited by sebtaw; 06-14-2018 at 04:41 PM.

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: number closest to 0

    Post a sample spreadsheet not an image (we cant manipulate them and Im not typing in all that data again). Supply expected results, remove any sensitive data, create a mockup example if necessary.
    Use the Go Advanced option at the bottom of the page then scroll down to Manage Attachments as the "paperclip" method does not work on this forum.
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    Try this in C2:

    =--(MIN(IF((A$2:A$38=A2)*(ABS(B$2:B$38)<=90),B$2:B$38))=B2) Ctrl Shift Enter

  4. #4
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Still figuring out how to attache a file

  5. #5
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by 63falcondude View Post
    Try this in C2:

    =--(MIN(IF((A$2:A$38=A2)*(ABS(B$2:B$38)<=90),B$2:B$38))=B2) Ctrl Shift Enter
    thanks, but I keep receiving an error notification when I fill in this formula. The 'B$2' gets highlighted as the main error.
    Last edited by sebtaw; 06-12-2018 at 11:30 AM.

  6. #6
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    Here is a non-CSE alternative:

    =--(AGGREGATE(15,6,(A$2:A$10=A2)*(ABS(B$2:B$10)<=90)*(B$2:B$10),1)=B2)


    I don't know what error notification you are receiving. It worked fine on my end.
    If you are using a non-english version of Excel, the function names may be different.

    Edit: Upon further testing, the AGGREGATE formula did not work in all cases.
    Last edited by 63falcondude; 06-12-2018 at 11:37 AM.

  7. #7
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    Quote Originally Posted by sebtaw View Post
    Still figuring out how to attache a file
    To upload an Excel workbook, follow these steps:
    1) Click on "Go Advanced"
    2) Click on "Manage Attachments"
    3) Click on "Choose File"
    4) Choose your file and click on "Open"
    5) Click on "Upload"
    6) Click on "Close this window"

  8. #8
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by 63falcondude View Post
    Here is a non-CSE alternative:

    =--(AGGREGATE(15,6,(A$2:A$10=A2)*(ABS(B$2:B$10)<=90)*(B$2:B$10),1)=B2)


    I don't know what error notification you are receiving. It worked fine on my end.
    If you are using a non-english version of Excel, the function names may be different.

    Edit: Upon further testing, the AGGREGATE formula did not work in all cases.
    Even after I translate the English to Dutch, neither formula seems to work

  9. #9
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    I have taken the formula from post #3 and adapted it to your workbook that you just shared in post #1. Open the workbook and you will see the formulas translated for you.

    I am assuming that this should be column by column and not be comparing all 3.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by 63falcondude View Post
    I have taken the formula from post #3 and adapted it to your workbook that you just shared in post #1. Open the workbook and you will see the formulas translated for you.

    I am assuming that this should be column by column and not be comparing all 3.
    It seemed to work fine, untill I sorted the patiënt bij number, and the formulas were not selecting the closes to zero anymore

  11. #11
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    Try this, unless I have misunderstood your goal it should work.

    EDIT: In place of zero I used a tack "-". This can easily be replaced with 0, but I figured it was easier to see the positive results without the 0's and only 1's.
    EDIT2: The only thing I hadnt tested for is a tie between 2 columns. In other words if 2 of the 3 columns are both the closest to 0, which do you put a 1 in (or do you do a 1 in both)?
    Attached Files Attached Files
    Last edited by Zer0Cool; 06-12-2018 at 11:57 AM.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  12. #12
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    thanks! it's one step closer to my goal at least. Per individual patient, all the numbers within the range of -90 to 90 are now selected, my goal is to select only the number that is closest to 0.
    Last edited by AliGW; 06-12-2018 at 12:06 PM. Reason: Unnecessary quotation removed.

  13. #13
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    Oh wait, ok you do want to do it per patient, but you have multiple records (rows) per patient as well? So for example M6 should have a 1 next to a number, but only 1 time across all the rows for M6?

  14. #14
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    yes, that's it pretty much
    Last edited by AliGW; 06-12-2018 at 12:06 PM. Reason: Unnecessary quotation removed.

  15. #15
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    Heres the updated copy, its an array formula now but should do what you want.

    EDIT: I forgot to fill the formula in column G down, sorry
    Attached Files Attached Files
    Last edited by Zer0Cool; 06-12-2018 at 12:08 PM.

  16. #16
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    Quote Originally Posted by sebtaw View Post
    It seemed to work fine, untill I sorted the patiënt bij number, and the formulas were not selecting the closes to zero anymore
    I sorted column A (I'm assuming this is the "patiënt bij number") and the formulas still work the same as before column A was sorted.

    Are you sure that the formulas (from post #9) produced the expected results before sorting?

  17. #17
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,916

    Re: number closest to 0

    @ Sebtaw:

    Please don't quote whole posts, especially when you are responding to the one immediately preceding your own - it's just clutter. It's OK to quote if you are responding to a post out of sequence, but limit quoted content to a few relevant lines that makes clear to whom and what you are responding. Thanks!

    For normal conversational replies, try using the QUICK REPLY box below.
    Last edited by AliGW; 06-12-2018 at 01:20 PM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  18. #18
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    I caught some errors, ill post an updated file shortly

  19. #19
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    Ok so I updated the formula, filled it down the columns and added a filter to your data to make checking it easier. I added some additional columns to test the results and check when a patient doesnt return a 1 someplace that the formula is working correct. IE: M8 returns all 0's, this is because they have 1 record with no value within -90 to 90, thus working correct.
    Attached Files Attached Files

  20. #20
    Forum Expert 63falcondude's Avatar
    Join Date
    08-22-2016
    Location
    USA
    MS-Off Ver
    365
    Posts
    6,266

    Re: number closest to 0

    Were you referring to me Ali? I quoted one sentence from post #10 and put it into post #16...

    This thread is getting too sloppy, I'm backing out.

  21. #21
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by Zer0Cool View Post
    Ok so I updated the formula
    It doesn't seem to be working fully, in some rows it doens't take the number closest to zero, here for patient M1 only column T=2 has a number closest to zero selected.
    pic3.png

    Edit: the highlighted numbers ought to have a "1" as well, as for patient M1 these are the closest to zero in column T=1 and T=3

  22. #22
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,916

    Re: number closest to 0

    Quote Originally Posted by 63falcondude View Post
    Were you referring to me Ali? I quoted one sentence from post #10 and put it into post #16...
    No, sorry - I've amended my post.

  23. #23
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    @sebtaw, Ok thats likely my misunderstanding what you want. I am doing the closest to 0 for a patient across all columns (T=1, T=2 and T=3). I am to understand then it should be per patient for each column separately?

  24. #24
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Sorry, English is not my first language, but yes, per patient for each column separately

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

    Re: number closest to 0

    As this thread still appears to be unresolved, I'll post my attempt. Not great in terms of efficiency, but it works.

    Array confirmed with Shift Ctrl Enter

    =IF(ABS(B2)>90,0,IF(ABS(B2)=MIN(ABS(MAX(IF($A$2:$A$270=$A2,IF(B$2:B$270<0,B$2:B$270,-1E+100)))),MIN(IF($A$2:$A$270=$A2,IF(B$2:B$270>0,B$2:B$270,1E+100)))),1,0))

    @Zer0Cool, it is considered courteous to post any formula in the thread rather than just uploading the answer in a workbook, that way other people can see what has already been suggested without having to download multiple copies. No point in everyone posting the same incorrect suggestion.

  26. #26
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: number closest to 0

    ARRAY formula in H2 then drag down.Pl see note below.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  27. #27
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by Zer0Cool View Post
    Ok so I updated the formula, filled it down the columns and added a filter to your data to make checking it easier. I added some additional columns to test the results and check when a patient doesnt return a 1 someplace that the formula is working correct. IE: M8 returns all 0's, this is because they have 1 record with no value within -90 to 90, thus working correct.
    actually, this was so far the closest solutions. But I cant manage to use this formula for just one column

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

    Re: number closest to 0

    Where does the formula in post #25 give incorrect results?

  29. #29
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    @sebtaw I have updated the file and attached it. It is now as follows:

    Columns C, E and G show a 1 to indicate that row/record has the number closest to 0 between -90 to 90 for that patient ID in that column/set. Each of the 3 columns/sets are now evaluated independently of each other.

    Ex: My earlier attempt would give you a 1 for each patient only 1 time across all three columns, now its possible for a patient to have upto 3x 1's, 1 per column (t=1, t=2, t=3).
    Attached Files Attached Files

  30. #30
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    @Zer0Cool, this looks great! But when I entered that same formula to my actual excel file, in some place where I was supposed to get a "1" it says #VALUE? I'm still trying to find out why.

    On further note, the array formule that you wrote, is it from -90 through 90 or from -90 till 90?

  31. #31
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: number closest to 0

    Your post does not comply with Rule 8 of our Forum RULES.

    Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    https://www.mrexcel.com/forum/excel-...ml#post5087715

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    HTH
    Regards, Jeff

  32. #32
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: number closest to 0

    Quote Originally Posted by sebtaw View Post
    On further note, the array formule that you wrote, is it from -90 through 90 or from -90 till 90?
    The way I written the formula it should be -90 to 90 inclusive, ie: -90, -89, -88,... 88, 89, 90

    #Value means that there is likely something in your data that the formula cannot calculate, like a number as text. More than likely one or more of the numbers are stored as text, for which you cannot get the ABS of text and get #VALUE instead.

    Double check you data and ensure all the numbers being evaluated are actually numeric.

  33. #33
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: number closest to 0

    @Zer0Cool,

    Please do not respond further until the OP has complied with the crosspost.

  34. #34
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    I applied the same formula to a sheet with all of my 370 patients included, yet some numbers seem to not be included by the array formula somehow, eventhough they should be:

    This is the array formula I used:
    =ALS(ABS(B2)>91;"-";ALS(MIN(ALS($A$2:$A$17=A2;ABS($B$2:$B$17)))=ABS(B2);1;"-"))

    formula error.png

    uploading the file unto this post keeps giving an error, so I provided the file through a google drive link: https://drive.google.com/file/d/1yuZ...ew?usp=sharing

    I can't seem to figure out the problem here, any clue?

  35. #35
    Registered User
    Join Date
    06-12-2018
    Location
    NL
    MS-Off Ver
    2010
    Posts
    13

    Re: number closest to 0

    Quote Originally Posted by jeffreybrown View Post
    @Zer0Cool,

    Please do not respond further until the OP has complied with the crosspost.
    I have provided a link to my crosspost in my first post, I will request the post on the other forum to be closed.

  36. #36
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: number closest to 0

    Quote Originally Posted by sebtaw View Post
    I have provided a link to my crosspost in my first post
    Thanks for the update.

+ 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. Closest Number above and below
    By robbie010 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-27-2014, 06:09 AM
  2. [SOLVED] Cell with text and number, not rounding to closest full number
    By cblp in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-03-2014, 07:51 PM
  3. Replies: 13
    Last Post: 08-13-2013, 12:51 PM
  4. help with ranking using closest number to number selected to break a tie
    By Drastic in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-25-2013, 02:43 PM
  5. Closest Number
    By scantor145 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2010, 11:34 AM
  6. How can I match a random number with closest number from sequence?
    By matt in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-02-2006, 08:25 PM
  7. How can i round a number to closest tenth number?
    By rayne95 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-19-2006, 04:40 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