Question:
EXCEL PLease HELPPP, Function ?????????????
luay_hannouna
2009-04-02 09:19:53 UTC
A have a range of numbers . and i am searching for a FUNCTION that makes the following.
I should enter a number and the function should extracts ( returns) the closest number from the data range
Six answers:
Cozmosis
2009-04-02 10:35:14 UTC
Here is a custom function to get the closest value from a data range



If your data range is A1:A20 and the look-up value is B1

=Closest(B1, A1:A20)



To install the custom function:

Alt+F11 to open the VB editor

Select the Insert menu\ Module

Paste the code below in the VB edit window



---



Function Closest(rngVal As Range, rngData As Range)

Application.Volatile

Dim temp As Double

Dim Cell As Range

'

temp = 1.79769313486231E+308

For Each Cell In rngData

If IsNumeric(Cell.Value) Then

If Abs(Cell.Value - rngVal.Value) < temp Then

temp = Abs(Cell.Value - rngVal.Value)

Closest = Cell.Value

End If

End If

Next Cell

End Function



----



In the case where your number is equal distant from two values in the data range, (e.g 15 is equal distant between 10 and 20), the Closest function will return the first value in the data range.
robert m
2009-04-02 16:28:11 UTC
Use IF function for each number in the range. Compare each differences, return only the smallest cell
2009-04-02 16:26:12 UTC
Look in the help file for VLOOKUP and HLOOKUP.



One of those might do what you want.





If not, try posting your question at http://www.mrexcel.com



They have a good forum there where you can get expert Excel help.
Marian
2009-04-02 16:48:31 UTC
Assume you put the range of numbers in column B (B1..B8 f.i.) and the number you seek in C1, then do the following:



in A1 put =ABS(B1-$C$1) and copy it down (up to A8 f.i.).



than use formula

=VLOOKUP(SMALL(A1:A8,1),A1:B8,2,0)

to get your number
voyager
2009-04-02 21:48:50 UTC
Say your data range is B1:B14 & you enter the number in A2.



Try this formula. It is an array formula. So press Ctrl+Shift+Enter instead of just Enter when you OK it.





=INDEX(B1:B14,MATCH (MIN(ABS(B1:B14-A2)),ABS (B1:B14-A2),0))
chris b
2009-04-02 16:24:51 UTC
with the exception of a big long IF statement the best i could think of would be max or min but those won't help you sorry.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...