Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Function MinDist(XY1 As Variant, XYRange As Variant) As Variant
Dim i As Long, j As Long, MDRes(1 To 1, 1 To 4) As Double, Dsq As Double, MinD As Double
Dim Mini As Long, Numrows As Long, STime As Single
' Find the minimum distance from XY1 to any point in the range XYRange
If TypeName(XY1) = "Range" Then XY1 = XY1.Value2
If TypeName(XYRange) = "Range" Then XYRange = XYRange.Value2
Numrows = UBound(XYRange)
For i = 1 To Numrows
Dsq = (XY1(1, 1) - XYRange(i, 1)) ^ 2 + (XY1(1, 2) - XYRange(i, 2)) ^ 2
If i = 1 Then
MinD = Dsq
Mini = 1
ElseIf Dsq < MinD Then
MinD = Dsq
Mini = i
End If
Next i
MDRes(1, 1) = Mini
MDRes(1, 2) = MinD ^ 0.5
MDRes(1, 3) = XYRange(Mini, 1)
MDRes(1, 4) = XYRange(Mini, 2)
MinDist = MDRes
End Function