+ Reply to Thread
Results 1 to 9 of 9

Find the closest match to a cell , but with a condition.

  1. #1
    Forum Contributor
    Join Date
    05-18-2011
    Location
    Delhi , India
    MS-Off Ver
    Excel 2007
    Posts
    171

    Find the closest match to a cell , but with a condition.

    Please see in the workbook , it would be easy to understand.
    thanks
    Attached Files Attached Files
    Last edited by Sarangsood; 12-31-2011 at 07:54 AM.

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Find the closest match to a cell , but with a condition.

    =SUM(IF(($B$3:$B$23>0)*ABS($A$3:$A$23-$F$1)=MIN(ABS($A$3:$A$23*($B$3:$B$23>0)-$F$1)),$A$3:$A$23))
    confirmed with CTRL + SHIFT + ENTER

    same can be applied for M:N by modifying ranges accordingly.
    (above would return 4800 & 4500 respectively)

  3. #3
    Forum Contributor
    Join Date
    05-18-2011
    Location
    Delhi , India
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Find the closest match to a cell , but with a condition.

    thanks a lot sir. you are a genius.

  4. #4
    Forum Contributor
    Join Date
    05-18-2011
    Location
    Delhi , India
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Find the closest match to a cell , but with a condition.

    just one more thing @XLENT , i would also need the second closest positive number from both the ranges.

  5. #5
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Find the closest match to a cell , but with a condition.

    Revert to SMALL from MIN such that you can adjust "k"

    =SUM(IF(($B$3:$B$23>0)*ABS($A$3:$A$23-$F$1)=SMALL(ABS($A$3:$A$23*($B$3:$B$23>0)-$F$1),2),$A$3:$A$23))
    confirmed with CTRL + SHIFT + ENTER

    would return 5200 & 4300 for A:B, M:N respectively

  6. #6
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Find the closest match to a cell , but with a condition.

    Just out of curiosity. Why does the result = 10000 when the input = 5000? I am not too familiar with the logic behind array formula, trying my best to understand it.

  7. #7
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Find the closest match to a cell , but with a condition.

    Good spot. In that instance there will be 2 closest matches 4800 + 5200

    If OP wants just 4800 I would replace the outer SUM with MIN for first match or MAX for last match
    Last edited by XLent; 12-31-2011 at 03:50 AM.

  8. #8
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Find the closest match to a cell , but with a condition.

    Thanks for the explanation. Really impressed with the solution you provided.

  9. #9
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Find the closest match to a cell , but with a condition.

    In hindsight for sake of exact match (ABS result is 0) we must insert a pre-emptive test

    =MIN(IF($B$3:$B$23>0,IF(ABS($A$3:$A$23-$F$1)=SMALL(ABS($A$3:$A$23*($B$3:$B$23>0)-$F$1),1),$A$3:$A$23)))
    confirmed with CTRL + SHIFT + ENTER

    change k to 2 for 2nd smallest.

+ 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