Results 1 to 2 of 2

Thread: Find closest value in range

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    1

    Find closest value in range

    My code loops through lots of blocks of data that are separated with a 'd' (see attachment).

    The code loops through each block of data between the d's perfectly.

    But I can't get it to search each block - the code in red won't work no matter how I change it.

    What it should do is search the values in column D and find the one that's closest to the value in column N. Once it's done that, an X should appear in the same row in Column F - as per the bottom 2.

    My code is here:

    Code:
    Sub subd()
    
        Columns("F:N").Select
        Selection.ClearContents
      
        Dim rngFind As Range
        Dim strValueToPick As String
        Dim rngLook As Range
        Dim strFirstAddress As String
        Dim topRow As Long
        Dim botRow As Long
        Dim count As Integer
        Dim W As Integer
        
        Set rngLook = Range("B1:B" & Cells(Rows.count, "B").End(xlUp).Row)
        strValueToPick = "d"
        With rngLook
            Set rngFind = .Find(strValueToPick, LookIn:=xlValues, LookAt:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Do
                    topRow = rngFind.Row
                    Set rngFind = .FindNext(rngFind)
                    botRow = rngFind.Row - 1
                    If botRow - topRow > 1 Then
    
    count = botRow - topRow
    W = (count * (Range("D" & topRow + 1).Value)) / WorksheetFunction.Pi
    Range("D" & topRow + 1).Offset(0, 10).Value = W
    
    
    If Range("D" & topRow + 1).Offset(3, 0).Value < 20 Then
    
    ' +++++++++++++++++++++++++
    
        Dim d As Double, i As Long, j As Long
        Dim LR As Long, x
        d = 1000
        LR = botRow - topRow
        
        x = Range("D" & topRow + 1).Offset(0, 10).Value
    
        For i = 1 To LR + 1
            If Range("D" & i).Value = "" Then
                d = 1000
                x = Range("N" & i + 1).Value
                Range("f" & j).Value = "X"
            Else
                If Abs(Range("D" & i).Value - x) < d Then
                    j = i
                    d = Abs(Range("D" & i).Value - x)
                End If
            End If
        Next i
    
    ' +++++++++++++++++++++++++
    
    Else
    Range("D" & topRow + 1).Offset(2, 2).Value = "b1"
    End If
            End If
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
    
    End Sub
    Attached Images Attached Images  

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Find closest value in range

    try this:

    vb Code:
    1. Sub subd()
    2.  
    3.     Range("B1:B" & Cells(Rows.count, "B").End(xlUp).Row).Select
    4.    
    5.     Dim start_endRows() As Integer
    6.     ReDim Preserve start_endRows(0)
    7.     start_endRows(0) = 1
    8.    
    9.     Selection.Find(What:="d", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
    10.             xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    11.             False, SearchFormat:=False).Activate
    12.    
    13.     ReDim Preserve start_endRows(UBound(start_endRows) + 1)
    14.    
    15.     start_endRows(UBound(start_endRows)) = ActiveCell.Row
    16.    
    17.     If start_endRows(UBound(start_endRows)) = start_endRows(UBound(start_endRows) - 1) Then Exit Sub
    18.    
    19.     Do
    20.         ReDim Preserve start_endRows(UBound(start_endRows) + 1)
    21.         Selection.FindNext(After:=ActiveCell).Activate
    22.         start_endRows(UBound(start_endRows)) = ActiveCell.Row
    23.        
    24.         If start_endRows(UBound(start_endRows)) < start_endRows(UBound(start_endRows) - 1) Then
    25.             ReDim Preserve start_endRows(UBound(start_endRows) - 1)
    26.             Exit Do
    27.         End If
    28.    
    29.     Loop
    30.    
    31.     For x = 0 To UBound(start_endRows) - 1
    32.    
    33.         Dim findValue As Integer
    34.         findValue = Cells(start_endRows(x) + 1, 14).Value
    35.        
    36.         Dim closest As Integer
    37.         closest = -1
    38.        
    39.         Dim difference As Double
    40.         difference = 65536
    41.        
    42.         For r = start_endRows(x) + 1 To start_endRows(x + 1) - 1
    43.            If Abs(Cells(r, 4).Value - findValue) < difference Then
    44.             difference = Abs(Cells(r, 4).Value - findValue)
    45.             closest = r
    46.            End If
    47.         Next
    48.        
    49.         If findValue <> 0 Then
    50.             Cells(closest, 6).Value = "X"
    51.         End If
    52.    
    53.     Next
    54.  
    55. End Sub
    Last edited by .paul.; Jan 27th, 2009 at 02:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width