By: Lutfor Rahman Shimanto

In reply to Dustin.

Hello DUSTIN

Thanks for reaching out and posting your query. You want to add a smoothing option while interpolating. You can achieve the goal with Excel formulas. However, I am presenting an Excel VBA User-defined function where you can add a smoothing option.

Assume you have two user-defined functions for applying the Cubic Spline and Cubic Hermite Spline interpolations. You want to create another user-defined function that will have the same parameters as before, including another extra parameter for choosing the smoothing option. Let’s say if p is provided, it will apply the Cubic Spline interpolation. And if q is provided, it will apply the Hermite Spline interpolation. If the Choose Parameter is not provided, it will raise a warning.

Excel VBA Code:

Function InterpolateWithChoice(periodValue As Range, rateValue As Range, xValue As Range, choice As String) As String

If choice = "p" Then
InterpolateWithChoice = CubicSplineIntrp(periodValue, rateValue, xValue)
ElseIf choice = "q" Then
InterpolateWithChoice = HrmtSplines(periodValue, rateValue, xValue)
Else
MsgBox "Invalid Choice! Please use 'p' for Cubic Spline or 'q' for Hermite Spline.", vbExclamation, "Invalid Choice"
InterpolateWithChoice = "#Invalid Choice"
End If

End Function

Function CubicSplineIntrp(periodValue As Range, rateValue As Range, xValue As Range)
CubicSplineIntrp = "Cubic Spline"
End Function

Function HrmtSplines(periodValue As Range, rateValue As Range, xValue As Range)
HrmtSplines = "Hermite Spline"
End Function

OUTPUT:

1. Cubic Spline
Here, we are providing p as the Choose Parameter.

2. Hermite Spline
We are providing q as the Choose Parameter.

3. Error Handling
Now, we are not providing any of the Choose Parameters. As a result, a Warning Window appears.

Press OK => You will get an output like the following image.

Hopefully, the idea will help you. Good luck!

Regards
Lutfor Rahman Shimanto