0
TL; DR:有兩個函數如下。他們執行相同的任務,但最重要的是接受範圍和計數的起點,而不是最後一個需要固定範圍的起點。問題是,除非數據放置在同一張表中,否則頂層函數不起作用。這很煩人,更多的是因爲我不明白爲什麼。在vba中調用(用戶自定義)函數稱爲LINTERP
當我發現有人爲excel製作了一個體面的插補器時,我感到寬慰了,因爲它們只能在2個點之間變化,並且這不足以滿足我的測量要求。
我決定修改公式,以便它不使用剛性的「範圍」對象,但給出一個開始「單元格」(範圍),然後給出該範圍的維度的計數整數。
我的數據現在必須與計算在同一張紙上,否則它將不起作用。你可以看一看,看看這是爲什麼嗎?
我的功能(我標誌着|在行我已經改變)
| Function LINTERPX(x As Double, rXs As Range, rYs As Range, No As Integer) As Variant
| With ThisWorkbook
Dim i As Long ' index to rY
Dim dF As Double ' interpolation fraction
Dim v As Variant ' for each/loop control variable
| Dim rX As Range
| Dim rY As Range
| Set rX = Range(Cells(rXs.Row, rXs.Column), Cells(rXs.Row + No - 1, rXs.Column))
| Set rY = Range(Cells(rYs.Row, rYs.Column), Cells(rYs.Row + No - 1, rYs.Column))
| End With
For Each v In Array(rX, rY)
If v.Areas.Count > 1 Then GoTo Oops
If v.Rows.Count <> 1 And v.Columns.Count <> 1 Then GoTo Oops
If WorksheetFunction.Count(v) <> v.Count Then GoTo Oops
Next v
If rX.Count < 2 Then GoTo Oops
If rX.Count <> rY.Count Then GoTo Oops
dFrac x, rX, i, dF, IIf(rX(2).Value2 > rX(1).Value2, 1, -1)
LINTERPX = rY(i).Value2 * (1 - dF) + rY(i + 1).Value2 * dF
Exit Function
Oops:
LINTERPX = CVErr(xlErrValue)
End Function
原文:
Function LINTERP(x As Double, rX As Range, rY As Range) As Variant
' shg 1997-0606, 2009-0419
' 2009-0604 added option for descending sort
' Linear interpolator/extrapolator
' Interpolates rX to return the value of y corresponding to the given x
' rX and rY must be equal-length vectors
' rX must be sorted (ascending or descending, doesn't matter)
Dim i As Long ' index to rY
Dim dF As Double ' interpolation fraction
Dim v As Variant ' for each/loop control variable
For Each v In Array(rX, rY)
If v.Areas.Count > 1 Then GoTo Oops
If v.Rows.Count <> 1 And v.Columns.Count <> 1 Then GoTo Oops
If WorksheetFunction.Count(v) <> v.Count Then GoTo Oops
Next v
If rX.Count < 2 Then GoTo Oops
If rX.Count <> rY.Count Then GoTo Oops
dFrac x, rX, i, dF, IIf(rX(2).Value2 > rX(1).Value2, 1, -1)
LINTERP = rY(i).Value2 * (1 - dF) + rY(i + 1).Value2 * dF
Exit Function
Oops:
LINTERP = CVErr(xlErrValue)
End Function
不清楚你在問什麼。太多信息。你應該減少你的帖子,只保留必需品。這會增加你獲得幫助的機會。有關指導,請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) –
Set rX =和Set rY行可能導致此問題。 「範圍」是指活動工作表。請嘗試設置rX = rXs.Resize(No,1)'(與'rY'類似) – BrakNicku
@ Jean-FrançoisCorbett:我在頂部添加了一個TL; DR,它是否覆蓋了它? –