linear interpolation function vba code

xelhelp

New Member
Joined
Mar 6, 2011
Messages
28
Hi,

I have an interpolation function that I have tried to write in VBA. The results of the function are correct when the given X value (in my case sigma) is equal to or outside the bounds of the highest or lowest sigma. But when the given X value or sigma for which I'm trying to interpolate is between the highest and lowest sigma I get incorrect results. I'm not sure where the code is wrong or why its giving incorrect results for this subset of scenario. I'd appreciate any insight. I will include the code below.

Thanks





Code:
Function interpsigma(lowsigma As Double, lowsigma2 As Double, highsigma As Double, highsigma2 As Double, lowvol As Double, lowvol2 As Double, highvol As Double, highvol2 As Double, sigmas As Range, vols As Range, targetsigma As Double)

If targetsigma = lowsigma Then
    interpsigma = lowvol
    
ElseIf targetsigma = highsigma Then
    interpsigma = highvol
    
ElseIf targetsigma < highsigma And targetsigma > lowsigma Then
    Dim firstindexpos As Double
    Dim secondindexpos As Double
    Dim firstsigmavalue As Double
    Dim secondsigmavalue As Double
    Dim firstsigmavol As Double
    Dim secondsigmavol As Double
            
    
    firstindexpos = Application.WorksheetFunction.Match(targetsigma, sigmas, 1)
    firstsigmavalue = Application.WorksheetFunction.Lookup(targetsigma, sigmas)
        
    If firstsigmavalue = targetsigma Then
    secondindexpos = firstindexpos
    Else
    secondindexpos = firstindexpos + 1
    End If
    
    secondsigmavalue = Application.WorksheetFunction.Index(sigmas, secondindexpos)
    
    firstsigmavol = Application.WorksheetFunction.Lookup(targetsigma, sigmas, vols)
    secondsigmavol = Application.WorksheetFunction.Index(vols, secondindexpos)
    
    If firstindexpos = secondindexpos Then
    interpsigma = firstsigmavol
    Else
    interpsigma = ((targetsigma - firstsigmavalue) / (((secondsigmavalue - firstsigmavalue) * (secondsigmavol - firstsigmavol)) + firstsigmavol))
    End If
    
    
ElseIf targetsigma > highsigma Then
    interpsigma = (((highvol - highvol2) / (highsigma - highsigma2)) * (targetsigma - highsigma)) + highvol
    
ElseIf targetsigma < lowsigma Then
    interpsigma = (((lowvol - lowvol2) / (lowsigma - lowsigma2)) * (targetsigma - lowsigma)) + lowvol

End If


End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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
Back
Top