+ Reply to Thread
Results 1 to 21 of 21

check distance difference based on criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    check distance difference based on criteria

    Hi,

    I have a Point A to Point B with the distance.

    1. user can set a adjustable distance in cell J1. e.g 2000

    2. User select Point A.

    3. User select Point B

    4. Then list out all the Point C where distance diff between Point A and Point B all within 2000.

    any solution please ?

    Thanks


    Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    Am I missing something? The A and B ranges look identical.

  3. #3
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    maybe should just use column A,B ,C .
    E , F and G is the duplicate.


    Example i select "P01" , it list out the PA13...etc where < 2000.

    HTML Code: 
    Then i select "P04" , it also will list out same Points PA13, PB08 where it match above.

    HTML Code: 
    As you see, distance from P01 to PPA13 is 1873. where P04 to PA13 is 1945. But still within 2000.
    Last edited by okl; 02-08-2010 at 08:01 AM.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    OK, but what do you want to do with the results? If you just want to see them, couldn't you just use AutoFilter?

  5. #5
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    But i got all list of point Bs from P04 where < 2000.

    HTML Code: 

  6. #6
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    use pivot ??

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    I think I'm starting to understand. So you want to select e.g. PO1 and find all the rows where distance <2000. Then select another, e.g. PO4, again find all the rows where distance <2000 and then the result you want is the Pt Cs which are in both sets. Is that right?

  8. #8
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    exactly, sorry for my bad explanation.

  9. #9
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    Pivot table possible ?

  10. #10
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    OK try attachment - press the button.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    great, but if i try Pt A = P01 , PtB = P10 , distance = 800 , it prompt me run time error "1004"

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    That's because there are no points. Try adding the section in the starred lines to avoid the error:
    'Insert this bit
    *********************************
    If n = 0 Then
        MsgBox "No points found"
        Exit Sub
    End If
    *********************************
    'Bit below already there
    With Sheet2
        .UsedRange.Clear
        .Range("A1").Resize(n, 3) = vData3
    End With

  13. #13
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    sorry , another error.

    it distance = 500 , pt A = p01 , pt B = P04,

    run time error "13", type mismatch ?

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    Try this. Messy code, but I think we're lurching towards a destination.
    Sub x()
     
    Dim rData As Range, vData, vData2, vData3(), n As Long, r As Long
    Dim oDic1 As Object, oDic2 As Object
    
    Application.ScreenUpdating = False
    
    Set oDic1 = CreateObject("Scripting.Dictionary")
    Set oDic2 = CreateObject("Scripting.Dictionary")
    
    With Sheet1
        .AutoFilterMode = False
        .Range("A1").AutoFilter Field:=1, Criteria1:=.Range("J2").Value
        .Range("A1").AutoFilter Field:=3, Criteria1:="<" & .Range("J1").Value
        With .AutoFilter.Range
            On Error Resume Next
            Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rData Is Nothing Then
                vData = rData.Value
            Else
                GoTo Err
            End If
        End With
        Set rData = Nothing
        .Range("A1").AutoFilter Field:=1, Criteria1:=.Range("J3").Value
        With .AutoFilter.Range
            On Error Resume Next
            Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rData Is Nothing Then
                vData2 = rData.Value
            Else
                GoTo Err
            End If
        End With
        .AutoFilterMode = False
    End With
    
    ReDim vData3(1 To UBound(vData, 1) + UBound(vData2, 1), 1 To 3)
    
    For r = LBound(vData, 1) To UBound(vData, 1)
        If Not IsEmpty(vData(r, 2)) And Not oDic1.exists(vData(r, 2)) Then
            n = n + 1
            oDic1.Add vData(r, 2), n
        End If
    Next r
    n = 0
    For r = LBound(vData2, 1) To UBound(vData2, 1)
        If Not IsEmpty(vData2(r, 2)) And Not oDic2.exists(vData2(r, 2)) Then
            n = n + 1
            oDic2.Add vData2(r, 2), n
        End If
    Next r
    n = 0
    For r = LBound(vData, 1) To UBound(vData, 1)
        If oDic1.exists(vData(r, 2)) And oDic2.exists(vData(r, 2)) Then
            n = n + 1
            vData3(n, 1) = Sheet1.Range("J2") & "/" & Sheet1.Range("J3")
            vData3(n, 2) = vData(r, 2)
            vData3(n, 3) = vData(r, 3)
        End If
    Next r
    
    With Sheet2
        .UsedRange.Clear
        .Range("A1").Resize(n, 3) = vData3
    End With
    
    Application.ScreenUpdating = True
    Exit Sub
    
    Err:
    
    MsgBox "No points found"
    
    Application.ScreenUpdating = True
            
    End Sub

  15. #15
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    sorry , error again.

    Set Distance 800
    Pt A P01
    Pt B P04

  16. #16
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    Dearie me, I'm doing well here aren't I. Try adding the first line below which I inadvertently removed last time.
    If n = 0 Then GoTo Err
    
    With Sheet2
        .UsedRange.Clear
        .Range("A1").Resize(n, 3) = vData3
    End With

  17. #17
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    even i key 2000, it give me no point found too. point is available.

    Sub x()

    Dim rData As Range, vData, vData2, vData3(), n As Long, r As Long
    Dim oDic1 As Object, oDic2 As Object

    Application.ScreenUpdating = False

    Set oDic1 = CreateObject("Scripting.Dictionary")
    Set oDic2 = CreateObject("Scripting.Dictionary")

    With Sheet1
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=1, Criteria1:=.Range("J2").Value
    .Range("A1").AutoFilter Field:=3, Criteria1:="<" & .Range("J1").Value
    With .AutoFilter.Range
    On Error Resume Next
    Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not rData Is Nothing Then
    vData = rData.Value
    Else
    GoTo Err
    End If
    End With
    Set rData = Nothing
    .Range("A1").AutoFilter Field:=1, Criteria1:=.Range("J3").Value
    With .AutoFilter.Range
    On Error Resume Next
    Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not rData Is Nothing Then
    vData2 = rData.Value
    Else
    GoTo Err
    End If
    End With
    .AutoFilterMode = False
    End With

    ReDim vData3(1 To UBound(vData, 1) + UBound(vData2, 1), 1 To 3)

    For r = LBound(vData, 1) To UBound(vData, 1)
    If Not IsEmpty(vData(r, 2)) And Not oDic1.exists(vData(r, 2)) Then
    n = n + 1
    oDic1.Add vData(r, 2), n
    End If
    Next r
    n = 0
    For r = LBound(vData2, 1) To UBound(vData2, 1)
    If Not IsEmpty(vData2(r, 2)) And Not oDic2.exists(vData2(r, 2)) Then
    n = n + 1
    oDic2.Add vData2(r, 2), n
    End If
    Next r
    n = 0
    For r = LBound(vData, 1) To UBound(vData, 1)
    If oDic1.exists(vData(r, 2)) And oDic2.exists(vData(r, 2)) Then
    n = n + 1
    vData3(n, 1) = Sheet1.Range("J2") & "/" & Sheet1.Range("J3")
    vData3(n, 2) = vData(r, 2)
    vData3(n, 3) = vData(r, 3)
    End If
    Next r


    If n = 0 Then GoTo Err

    With Sheet2
    .UsedRange.Clear
    .Range("A1").Resize(n, 3) = vData3
    End With

    Err:

    MsgBox "No points found"

    Application.ScreenUpdating = True

    End Sub

    Last edited by okl; 02-09-2010 at 08:18 AM.

  18. #18
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    Which points? With P01/P04 I get 7 results.

  19. #19
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    Same point too?? 1and4

  20. #20
    Forum Contributor
    Join Date
    01-17-2008
    Posts
    156

    Re: check distance difference based on criteria

    Can attach your workbook for me to confirm again?


    or is the code correct in sequence?

    
    If n = 0 Then GoTo Err
    
    With Sheet2
    .UsedRange.Clear
    .Range("A1").Resize(n, 3) = vData3
    End With
    
    Err:
    
    MsgBox "No points found"
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by okl; 02-09-2010 at 09:09 AM.

  21. #21
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: check distance difference based on criteria

    See attachment. I'm losing confidence in this approach. Might be easier to go back to the drawing board.
    Attached Files Attached Files

+ 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