Hey, I am trying to simply find the closest numbers to a value of type double, both largest and smallest in a range.
<code>
Function smallest(searchValue As Double, searchRange As Range, searchWorksheet As String)
Dim searchLocation, locatedValue As Variant
With Application.Worksheets(searchWorksheet)
searchLocation = Application.CountIf(searchRange, "<" & searchValue))
MsgBox searchValue
MsgBox searchRange.Address
MsgBox searchLocation
MsgBox ThisWorkbook.ActiveSheet.Name
searchRange.Select
If IsError(searchLocation) = False Then
locatedValue = Application.Small(searchRange, searchLocation)
If IsError(locatedValue) = False Then
smallest = locatedValue
Else
MsgBox "error 2"
End If
Else
MsgBox "error 1"
End If
End With
End Function
Sub testSmallest()
MsgBox smallest(0.5, Range("C1:C61"), "Site1.Frequency.1")
End Sub
</code>
It's returning zero, when the column has 60 values 0.0001 -> 1 which are also sorted.
<code>
Function smallest(searchValue As Double, searchRange As Range, searchWorksheet As String)
Dim searchLocation, locatedValue As Variant
With Application.Worksheets(searchWorksheet)
searchLocation = Application.CountIf(searchRange, "<" & searchValue))
MsgBox searchValue
MsgBox searchRange.Address
MsgBox searchLocation
MsgBox ThisWorkbook.ActiveSheet.Name
searchRange.Select
If IsError(searchLocation) = False Then
locatedValue = Application.Small(searchRange, searchLocation)
If IsError(locatedValue) = False Then
smallest = locatedValue
Else
MsgBox "error 2"
End If
Else
MsgBox "error 1"
End If
End With
End Function
Sub testSmallest()
MsgBox smallest(0.5, Range("C1:C61"), "Site1.Frequency.1")
End Sub
</code>
It's returning zero, when the column has 60 values 0.0001 -> 1 which are also sorted.