Public Function Linterp3(x As Double, Tblx As Range, Tbly As Range) As Variant
' linear interpolator / extrapolator
' Tbl is a two-column range containing known x, known y, sorted x ascending Dim nRow As Long Dim iLo As Long, iHi As Long nRow = Tblx.Rows.Count If nRow < 2 Or Tblx.Columns.Count <> 1 Or Tbly.Columns.Count <> 1 Or Tblx.Ro ws.Count <> Tbly.Rows.Count Then Linterp3 = CVErr(xlErrValue) Exit Function '--------------------------------------------------------> End If If x < Tblx(1, 1) Then ' x < xmin, extrapolate from first two entries iLo = 1 iHi = 2 ElseIf x > Tblx(nRow, 1) Then ' x > xmax, extrapolate from last two entries iLo = nRow - 1 iHi = nRow Else iLo = Application.Match(x, Application.Index(Tblx, 0, 1), 1) If Tblx(iLo, 1) = x Then ' x is exact from table Linterp3 = Tbly(iLo, 1) Exit Function '----------------------------------------------------> Else ' x is between tabulated values, interpolate iHi = iLo + 1 End If End If Linterp3 = Tbly(iLo, 1) + (Tbly(iHi, 1) - Tbly(iLo, 1)) _ * (x - Tblx(iLo, 1)) / (Tblx(iHi, 1) - Tblx(iLo, 1)) End Function