=IFERROR(SUM((M4:M7-M3:M6)*N3:N6*--(K9>=M4:M7))+(K9-VLOOKUP(K9;M3:M7;1))*VLOOKUP(K9;M3:N7;2);0)
Function Alsqr(tRange As Range, Range As Range) As Variant
If Range <= 0 Then Alsqr = 0: Exit Function
Dim lr As Long
Dim sh As String
Dim rCount As Long
Dim st As Long
Dim ct As Long
Dim i As Long
Dim r As Variant
rCount = tRange.Rows.Count
st = tRange(1, 1).Row
ct = tRange(1, 1).Column
sh = tRange.Parent.Name
lr = rCount + st
With Sheets(sh)
For i = st + 2 To lr - 1
If .Cells(i, ct) < Range Then
r = r + ((.Cells(i, ct) - .Cells(i - 1, ct)) * .Cells(i - 1, ct + 1))
End If
If .Cells(i, ct) >= Range Then
r = r + ((Range - .Cells(i - 1, ct)) * .Cells(i - 1, ct + 1))
Exit For
End If
Next i
If Range > .Cells(lr - 1, ct) Then
r = r + ((Range - .Cells(lr - 1, ct)) * .Cells(lr - 1, ct + 1))
End If
End With
Alsqr = r: Exit Function
End Function