0% found this document useful (0 votes)
48 views2 pages

Interpolate

Excel macro for interpolating values

Uploaded by

Nabeel Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

Interpolate

Excel macro for interpolating values

Uploaded by

Nabeel Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Public Function Interpolate(x As Double, xArray As Range, yArray As Range) As

Double

'Variable specifications

Dim i As Integer, xSize As Integer, ySize As Integer


Dim x1 As Double, x2 As Double, y1 As Double, y2 As Double

'Application.Volatile

'Count the rows and columns

xSize = xArray.Rows.Count
ySize = xArray.Columns.Count

'Iterate through rows

If (xSize > 1) And (ySize = 1) Then


For i = 1 To xSize - 1
x1 = xArray(i, 1)
y1 = yArray(i, 1)
x2 = xArray(i + 1, 1)
y2 = yArray(i + 1, 1)

If (((x >= x1) And (x <= x2)) Or ((x >= x2) And (x <= x1))) Then

'Calculate y value corresponding to x from linear interpolaion

'MsgBox "x1=" & x1 & ", y1=" & y1 & ", x2=" & x2 & ", y2=" & y2 &
", x=" & x

Interpolate = y1 + (y2 - y1) * (x - x1) / (x2 - x1)

End If
Next i

'Iterate through columns

ElseIf (xSize = 1) And (ySize > 1) Then


For i = 1 To ySize - 1
x1 = xArray(1, i)
y1 = yArray(1, i)
x2 = xArray(1, i + 1)
y2 = yArray(1, i + 1)

If (((x >= x1) And (x <= x2)) Or ((x >= x2) And (x <= x1))) Then

'Calculate y value corresponding to x from linear interpolaion

'MsgBox "x1=" & x1 & ", y1=" & y1 & ", x2=" & x2 & ", y2=" & y2 &
", x=" & x

Interpolate = y1 + (y2 - y1) * (x - x1) / (x2 - x1)

End If
Next i
End If

End Function

You might also like