Linear Interpolation in VBA


New Member
Jul 22, 2019
Hey Everyone,

I have been working on a function to linear interpolate some data- I got it to where it was giving values however they seem to be incorrect. I cant find an error in the equation so far so i am not sure where to fix things.

I am also extremely new to VBA to bear with me on the code

Thanks for the help in advance!

Dim X1 As Double, X2 As Double, X3 As Double, Y1 As Double, Y3 As Double

' X1 is used as the smallest x value
X1 = ActiveCell.Value
' X is used as the middle value
Application.ActiveCell.offset(1, 0).Activate
X = ActiveCell.Value
' X2 is used as the largst x value
Application.ActiveCell.offset(1, 0).Activate
X2 = ActiveCell.Value
' Y1 is used as the smallest Y value
Application.ActiveCell.offset(-2, 1).Activate
Y1 = ActiveCell.Value2
' Y2 is used as the largest Y value
Application.ActiveCell.offset(2, 0).Activate
Y2 = ActiveCell.Value2

Dim Pt1 As String, Pt2 As String, Pt3 As String

Pt1 = (X - X1)
Pt2 = (Y2 - Y1)
Pt3 = (X2 - X1)

MsgBox "" & Y1 + Pt1 * (Pt2 \ Pt3)

End Function

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.


MrExcel MVP
May 17, 2006
welcome, Annie

Please post a sample of the input data. What values are in what cells, and maybe describe what each value means if it might aid understanding.

And then explain what the result is for those inputs and where/how it will be used.

regards, Fazza


Well-known Member
May 1, 2002
Office Version
Can you also post the first part of your code - possibly only one line from the top (Public Function, perhaps?
Also, where does this code reside? in a code module, or worksheet module, or ThisWorkbook module?

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...