Does Excel Have an Interpolation Function ?

orekin

Board Regular
Joined
Jun 1, 2004
Messages
50
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
Master Excel Bundle

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.

Forum statistics

Threads
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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Top