我尝试插入的数据如下所示:
=X=
0
0
0
0
0
0.5
1
3.1
5.5
6.2
8.5
=Y=
0.019607843
0.019607843
0.019607843
0.019607843
0.019607843
0.117647059
0.137254902
0.156862745
0.176470588
0.196078431
0.215686275
=Z=
0.019607843
0.039215686
0.058823529
0.078431373
0.098039216
0.117647059
0.137254902
0.156862745
0.176470588
0.196078431
0.215686275
0.235294118
我正在使用http://www.xlxtrfun.com/XlXtrFun/XlXtrFun.htm加入。在一个单元格中,我有来自该插件的插值公式,除了这组特定的数据之外,它在大多数情况下都可以正常工作。
在AM1中我有以下代码:
=Interpolate($F$2:$F$51,$D$2:$D$51,M2,TRUE,FALSE)
其中 F 列是 Y 数据 D 是 X 数据,M 是 Z
问题在于,在 X 组数据中存在连续的 0 值,因此在包含公式的单元格中我得到一个 #NUM!
我尝试将 0 稍微更改为 0.1 0.2 等。之后它工作正常,所以我知道公式是正确的,但是使用这些数据我无法更改它,否则它最终会极大地改变结果。那么有谁知道我可以保留这些值并保持正确插值的方法吗?
最佳答案
无论如何,如果您想尝试将上述公式作为 VBA 函数(以便保持工作表干净)...您可以尝试以下操作。也许有一天我不想编写更多代码;)所以它对我来说有效。如果它对你有用,那么一切都归功于 Tushar Mehta哈哈,我刚刚从@pnuts 复制了该标签行。但是在上面的站点中,您有更多与国际刑警组织相关的代码。也请尝试一下。
Option Explicit
Option Compare Text
Function RealEqual(X, Y) As Boolean
RealEqual = Abs(X - Y) <= 0.00000001
End Function
Function LinearInterp(XVals, YVals, TargetVal)
Dim MatchVal
On Error GoTo ErrXit
With Application.WorksheetFunction
MatchVal = .Match(TargetVal, XVals, 1)
If MatchVal = XVals.Cells.Count _
And RealEqual(TargetVal, .Index(XVals, MatchVal)) Then
LinearInterp = .Index(YVals, MatchVal)
Else
LinearInterp = .Index(YVals, MatchVal) _
+ (.Index(YVals, MatchVal + 1) - .Index(YVals, MatchVal)) _
/ (.Index(XVals, MatchVal + 1) _
- .Index(XVals, MatchVal)) _
* (TargetVal - .Index(XVals, MatchVal))
End If
End With
Exit Function
ErrXit:
With Err
LinearInterp = .Description & "(Number= " & .Number & ")"
End With
End Function
工作 TableView :
关于vba - Excel 插值法使用连续顺序的相同值的几个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272723/