Advertisement
caffeine314

Untitled

Jun 15th, 2025
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. Public Function InterpolatedRate(targetDate As Date, dataRange As Range) As Variant
  4.     Dim datesRange As Range, ratesRange As Range
  5.     Dim idx As Variant
  6.     Dim date1 As Date, date2 As Date
  7.     Dim rate1 As Double, rate2 As Double
  8.  
  9.     ' Make sure there are two columns.
  10.    If dataRange.Columns.Count <> 2 Then
  11.         InterpolatedRate = CVErr(xlErrRef)
  12.         Exit Function
  13.     End If
  14.  
  15.     ' Set dataRange = TrimEmptyRows(dataRange)
  16.  
  17.  
  18.  
  19.     Set datesRange = dataRange.Columns(1)
  20.     Set ratesRange = dataRange.Columns(2)
  21.  
  22.     ' Make sure targetDate is a date
  23.    If Not IsDate(targetDate) Then Debug.Print "targetDate not a date: " & targetDate
  24.  
  25.     ' Make sure the target date is AFTER the first date in our range.
  26.    Dim firstDate As Date: firstDate = datesRange.Cells(1, 1).Value
  27.     If targetDate < firstDate Then Debug.Print "targetDate < dateRange: " & DateRange.Cells(1, 1).Value
  28.  
  29.     ' Make sure everything is a date.
  30.    For Each c In datesRange
  31.         If IsDate(c.Value) Then c.Value = CDate(c.Value)
  32.     Next
  33.  
  34.     On Error Resume Next
  35.     idx = Application.Match(targetDate, datesRange, 1)
  36.     On Error GoTo 0
  37.  
  38.     ' Can't combine this with below because of short circuiting behavior.
  39.    'If IsError(idx) Then
  40.    '    InterpolatedRate = CVErr(xlErrNA)
  41.    '    Exit Function
  42.    'End If
  43.  
  44.     'date1 = datesRange.Cells(idx, 1).Value
  45.    'date2 = datesRange.Cells(idx + 1, 1).Value
  46.    'rate1 = ratesRange.Cells(idx, 1).Value
  47.    'rate2 = ratesRange.Cells(idx + 1, 1).Value
  48.  
  49.     ' InterpolatedRate = LinearInterpolate(CDbl(targetDate), CDbl(date1), CDbl(date2), rate1, rate2)
  50. End Function
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement