Bernard,
Thanks for the helpful response.
Your approach was exactly what I was looking for. Although, for some
reason, I did have a problem setting the "mymin" initial value as 1E+15 even
though I dimensioned the variable as "long". I just set it to 10,000
believing that I will not have more than 10,000 coordinate points to deal
with.
Thanks again.
-Bill
"Bernard Liengme" wrote:
Hello Bill,
Flyswift has given a good answer if it is the minimum distance you need.
If you need the coordinates of the nearest 'actual' point then this UDF will
help
I had Design value in A3:B20, Actual values in E3:F20 (but anywhere works
and, of course, the ranges can be longer.
To find the Actual closes to the first Design point I called the UDF with
=Closest(A3:B3,$E$3:$F$20)
This retunes the index of the closest point. If this result is in J3 then
=INDEX(E$3:E$7,$J3) returns the x-value of the closes point while
=INDEX(F$3:F$7,$J3) returns the y-values
The subroutine is not very sophisticated but it works.
+++++++++++++++
Function closest(design As Range, actual As Range)
mymin = 1E+15
mycount = actual.Count
Dim mydesign(2)
ReDim myactual(mycount, 2)
mydesign(1) = design(1)
mydesign(2) = design(2)
k = 1
For j = 1 To mycount Step 2
myactual(k, 1) = actual(j)
myactual(k, 2) = actual(j + 1)
k = k + 1
Next j
mycount = mycount / 2
For k = 1 To mycount
distance = Sqr((mydesign(1) - myactual(k, 1)) ^ 2 + (mydesign(2) -
myactual(k, 2)) ^ 2)
If distance < mymin Then
mymin = distance
myindex = k
End If
Next k
closest = myindex
End Function
+++++++++++++++++++++++++++
If you are new to VBA look at: David McRitchie's site on "getting started"
with VBA
http://www.mvps.org/dmcritchie/excel/getstarted.htm
best wishes
--
Bernard V Liengme
www.stfx.ca/people/bliengme
remove caps from email
"Bill" wrote in message
...
I have two arrays of (x,y) coordinates. Each with roughly 500 pairs. Call
one "Design" and the other "Actual". I need to find the distance from
each
Actual coordinate to it's closest Design coordinate. Problems: 1). There
is
low probability that any of the Actual coordinates with exactly match the
Design coordinates. 2). The arrays are not ordered anywhere near to
coorespondence (ie. Design coord x1,y1 will not necessarily coorespond
with
Actual coord x1,y1). 3). Some Design coordinates will not have any
Actual
matches.
Difficult enough? ;)