# Does Excel Have an Interpolation Function ?

#### orekin

##### Board Regular
Hi

I have written a UDF that does simple linear interpolation. The code is pasted below.

Am I reinventing the wheel ? Does excel have a function that does this ?

Cheers
Orekin

Code:
``````Public Function XL_LinearInterpolate(xValue As Double, inputRange As Range) As Double
Dim xyValues() As Double
Call CopyRangeToDoubleArray(inputRange, xyValues())
XL_LinearInterpolate = LinearInterpolate(xValue, xyValues())
End Function

Public Function LinearInterpolate(xValue As Double, xyValues() As Double) As Double

'Assumes that:
'dataset() is sorted ascending by first column
'x vals in first column, y values in second column

Dim i As Long

'If outside range, then flat line it
If (xValue <= xyValues()(1, 1)) Then
LinearInterpolate = xyValues()(1, 2)
Exit Function
ElseIf (xValue >= xyValues()(UBound(xyValues), 1)) Then
LinearInterpolate = xyValues()(UBound(xyValues), 2)
Exit Function
End If

'Check within the range
For i = 1 To UBound(xyValues()) - 1 Step 1
If (xValue >= xyValues()(i, 1) And xValue <= xyValues()(i + 1, 1)) Then
LinearInterpolate = xyValues(i, 2) + (xyValues(i + 1, 2) - xyValues(i, 2)) * (xValue - xyValues(i, 1)) / ((xyValues(i + 1, 1) - xyValues(i, 1)))
Exit Function
End If
Next i
End Function``````

### Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

##### MrExcel MVP

Replies
3
Views
600
Replies
4
Views
183
Replies
1
Views
154
Replies
16
Views
465
Replies
0
Views
255

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,163,812
Messages
5,833,801
Members
430,233
Latest member
cbutts

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back