+ Reply to Thread
Results 1 to 10 of 10

Finding minimum difference of all elements in one row or column?

  1. #1
    Registered User
    Join Date
    01-08-2009
    Location
    Canada
    MS-Off Ver
    Excel 2000
    Posts
    12

    Finding minimum difference of all elements in one row or column?

    Hello,

    I need to find the minimum difference between any two elements in a row or a column. While it's easy to do for a 3-4 elements by doing subtractions for all elements in the array, doing it for more elements leads to a very long formula.
    For example, I need to find the difference between any two elements between C5 and C9:

    =MIN(ABS(C5-C6), ABS(C5-C7), ABS(C5-C8), ABS(C5-C9), ABS(C6-C7), ABS(C6-C8), ABS(C6-C9), ABS(C7-C8), ABS(C7-C9), ABS(C8-C9))

    Is there a better way of doing it?
    MIN(ABS(C5:C8-C6:C9)) finds the minimum difference of two consecutive elements, so I can't use that (OK, I can reduce a few operations, but that's not the point).


    Thank You in advance.
    Last edited by J10; 01-28-2009 at 04:12 PM.

  2. #2
    Forum Contributor ptm0412's Avatar
    Join Date
    04-16-2008
    Location
    Vietnam
    MS-Off Ver
    Office 2003 and 2007
    Posts
    129

    Re: Finding minimum difference of all elements in one row or column?

    If you see that the Minimum diffrence within a range is also the difference beween the smallest one and the second one, it will be easy. Try this:

    =SMALL(C5:C9,2)-MIN(C5:C9)

  3. #3
    Registered User
    Join Date
    01-08-2009
    Location
    Canada
    MS-Off Ver
    Excel 2000
    Posts
    12

    Re: Finding minimum difference of all elements in one row or column?

    Thanks, but I don't know whether the difference between the smallest one and the second one will be the minimum. It can as well be the difference between the largest element and the second largest element; or between some elements in the middle range.

  4. #4
    Forum Contributor ptm0412's Avatar
    Join Date
    04-16-2008
    Location
    Vietnam
    MS-Off Ver
    Office 2003 and 2007
    Posts
    129

    Re: Finding minimum difference of all elements in one row or column?

    Sorry for that, J10. Your request could be solve with an UDF:
    PHP Code: 
    Option Base 1
    Function MinofDiff(Rng)
    Dim ArrRng
    Dim TempDiff
    Set ArrRng 
    Rng
    MinofDiff 
    Application.Max(ArrRng)
    For 
    1 To ArrRng.Count 1
    For 1 To ArrRng.Count
        TempDiff 
    Abs(ArrRng(i) - ArrRng(j))
        
    MinofDiff Application.Min(TempDiffMinofDiff)
    NextNext
    End 
    Function 
    Note: This Function is used for single row or single column only.
    Attached Files Attached Files
    Last edited by ptm0412; 01-27-2009 at 10:51 PM.

  5. #5
    Registered User
    Join Date
    01-08-2009
    Location
    Canada
    MS-Off Ver
    Excel 2000
    Posts
    12

    Re: Finding minimum difference of all elements in one row or column?

    Thank You. Would user-defined function work with older versions of Excel? E.g. Excel 2000.
    Last edited by shg; 01-27-2009 at 11:17 PM. Reason: deleted spurious quote

  6. #6
    Forum Contributor ptm0412's Avatar
    Join Date
    04-16-2008
    Location
    Vietnam
    MS-Off Ver
    Office 2003 and 2007
    Posts
    129

    Re: Finding minimum difference of all elements in one row or column?

    I have just attached the file, please try. I think it is no problem using UDF in Excel 2000.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Finding minimum difference of all elements in one row or column?

    J10, please don't quote whole posts -- it just clutters the forum.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Finding minimum difference of all elements in one row or column?

    I believe this array formula works if all values are unique:

    =MIN(IF( (C5:C9 - TRANSPOSE(C5:C9)) < 0, ABS(C5:C9 - TRANSPOSE(C5:C9) ) ) )

    Array formulas MUST be confirmed with Ctrl+Shift+Enter, not Enter. You'll know you did it correctly if curly braces appear around the formula in the Formula Bar; you cannot type in the braces directly.

  9. #9
    Registered User
    Join Date
    01-08-2009
    Location
    Canada
    MS-Off Ver
    Excel 2000
    Posts
    12

    Re: Finding minimum difference of all elements in one row or column?

    Thank You, ptm0412.
    Shg, the values are not unique, hence that won't work for me. Thanks though.
    P.S. I do know about array formulas.

    Thank You again.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Finding minimum difference of all elements in one row or column?

    If the values are not unique, then the minimum difference is zero.

+ 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