Finding min difference between any of the values in a list values

jlapping

New Member
Joined
Jun 3, 2010
Messages
2
I have about 100 values in column some of which are the same and want to retrieve the smallest difference between any of the values in the list.

For the example of the list of numbers below I would like to retrieve the answer = 0.04

[TABLE="width: 64"]
<colgroup><col></colgroup><tbody>[TR]
[TD="align: right"]15.03[/TD]
[/TR]
[TR]
[TD="align: right"]15.11[/TD]
[/TR]
[TR]
[TD="align: right"]15.22[/TD]
[/TR]
[TR]
[TD="align: right"]15.35[/TD]
[/TR]
[TR]
[TD="align: right"]15.16[/TD]
[/TR]
[TR]
[TD="align: right"]15.48[/TD]
[/TR]
[TR]
[TD="align: right"]16.25[/TD]
[/TR]
[TR]
[TD="align: right"]16.08[/TD]
[/TR]
[TR]
[TD="align: right"]15.26[/TD]
[/TR]
</tbody>[/TABLE]

Thanks for any suggestions or ideas...
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I think this works but haven't done any thorough testing:


Excel 2010
ABC
115.030.04
215.11
315.22
415.35
515.16
615.48
716.25
816.08
915.26
Sheet1
Cell Formulas
RangeFormula
C1=MinAll(A1:A9)


Code:
Option Explicit
Function MinAll(rng As Range)
Dim c As Range
Dim vCol As Collection
Dim i As Long
Dim MinTemp(1 To 3) As Variant
MinTemp(1) = 0
MinTemp(2) = 0
MinTemp(3) = 999999999
Set vCol = New Collection


On Error Resume Next
For Each c In rng
    vCol.Add c.Value, CStr(c.Value)
Next c
On Error GoTo 0


Call nRecursion(vCol, 1, MinTemp)


MinAll = MinTemp(3)


End Function
Function nRecursion(vCol As Collection, cIndex As Long, MinTemp() As Variant) As Variant
Dim i As Long
    If cIndex < vCol.Count - 1 Then
        Call nRecursion(vCol, cIndex + 1, MinTemp)
    End If
        Debug.Print "Loop for i = " & cIndex & " to " & vCol.Count
        For i = cIndex To vCol.Count
            If Abs(vCol(cIndex) - vCol(i)) <> 0 And Abs(vCol(cIndex) - vCol(i)) < MinTemp(3) Then
                MinTemp(1) = vCol(cIndex)
                MinTemp(2) = vCol(i)
                MinTemp(3) = Abs(vCol(cIndex) - vCol(i))
            End If
        Next i
End Function
 
Upvote 0
Thanks guys to both of you for taking the time to reply.

I ended up going with the VBA solution and it worked a treat.

Jason
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top