Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations pierreick on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match Index with Tolerances

Status
Not open for further replies.

Trenno

Structural
Feb 5, 2014
831
Hi,

I'd like to set up a function that compares the locations in the left box with the Master locations in the right box. The function will return the Master location if a match is found.

I've managed to break down the locations by X and Y coordinates.

The test would be something like IF Dist < Tolerance THEN Returns corresponding master location. The function would test all of the master locations.

Dist = SQRT ( |X1-X2|^2 + |Y1-Y2|^2 )

locations_yt014b.png


Any help would be much appreciated.
 
Replies continue below

Recommended for you

Hi,

Assumptions:

[PRE]
A B C D E
MASTER tol 0.5
x y x2 y2 mtch
2.683 19.24 2.437 19.16 [highlight #8AE234]2.683:19.24[/highlight]
3.043 22.16 3.015 22.07 3.043:22.16
4.998 13.35 4.998 13.26 4.998:13.35

[/PRE]

The ranges in MASTER are named x & y

The funcion to calculate mtch, in a standard module
Code:
Function mtch(x2 As Single, y2 As Single, tol As Single)
    Dim rx As Range
    Dim x1 As Single, y1 As Single
    Dim dist As Single
'Dist = SQRT ( |X1-X2|^2 + |Y1-Y2|^2 )
    
    For Each rx In [x]
        x1 = rx.Value
        y1 = rx.Offset(0, 1).Value
        
        dist = ((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ 0.5
        
        If dist < tol Then Exit For
    Next
    
    mtch = x1 & ":" & y1
End Function

Use the mtch function as you would any other function...
[tt]
E12: [highlight #8AE234]=mtch(C12,D12,tol)[/highlight]
[/tt]


Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Thanks for the really helpful post, SV.

However, I'm getting a #VALUE error when attempting to replicate it. Do you need to add into the function code something that defines the 'tol'?



 
tol is the cell containing 0.5. I have that cell named tol.

Making prolific use of Named Ranges is a wise and beneficial practice.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Why do you need to use the ABS function? A number squared is always a positive number.
 
You are correct. Wasn’t thinking.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Note, if you have more than one match, the code given by skipVought would only return the first match. This might occur for example if you have a larger tolerance compared to the spacing of points. Two closely spaced points would only return the first match which may or may not be the correct point.

It would be pretty easy to extend the code given to return all matches as an array which is then written to the sheet. Or alternatively return the point with the least distance.
 
In that case, the OP ought to post test data and expected results that illustrate that condition.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
I actually want it to return the first match in the master list, so no problems there.

Got it to work fine, that's for the help SV.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor