#在Excel中使用自定义函数的错误值

当我尝试调用自定义函数时出现#VALUE错误。 所有它应该做的是一些math。 有没有人看到这里可能是错的?

我从互联网上复制这一个:

资源:

Linear Interpolation VBA Function in Excel

Function LININTERP(x, xvalues, yvalues) 'x and y values must be in ascending order from top to bottom. 'x must be within the range of available data. x1 = Application.WorksheetFunction.Index(xvalues, Application.WorksheetFunction.Match(x, xvalues, 1)) x2 = Application.WorksheetFunction.Index(xvalues, Application.WorksheetFunction.Match(x, xvalues, 1) + 1) y1 = Application.WorksheetFunction.Index(yvalues, Application.WorksheetFunction.Match(x, xvalues, 1)) y2 = Application.WorksheetFunction.Index(yvalues, Application.WorksheetFunction.Match(x, xvalues, 1) + 1) LININTERP = y1 + (y2–y1) * (x–x1) / (x2–x1) End Function 

这是我认为工作表函数调用可能导致错误的简化版本:

 Function LININTERP(x, x1, x2, y1, y2) LININTERP = y1 + (y2–y1) * (x–x1) / (x2–x1) End Function 

我的testing数据在一个不相关的工作簿中:(全部格式化为“常规”)

 A1: 633 A2: 634 B1: 14.968 B2: 15.024 C1 (my x): 633.6 

只要将实际的math运算插入一个单元格就可以按照预期工作。 调用该函数将引发#VALUE错误。

我的function被保存在一个工作簿中的一个模块中,我已经保存并作为一个加载项添加到Excel中。

我对您的公式和数据的抽样在连字符上出现错误,不会被解释为“负号”。 实际上,它们是以unicode 8211的forms出现的。重新调整它们,将variables声明为variables并删除...WorksheetFunction...修复了这个问题。

 Function LININTERP(x, xvalues, yvalues) Dim x1 As Variant, x2 As Variant, y1 As Variant, y2 As Variant 'x and y values must be in ascending order from top to bottom. 'x must be within the range of available data. x1 = Application.Index(xvalues, Application.Match(x, xvalues, 1)) x2 = Application.Index(xvalues, Application.Match(x, xvalues, 1) + 1) y1 = Application.Index(yvalues, Application.Match(x, xvalues, 1)) y2 = Application.Index(yvalues, Application.Match(x, xvalues, 1) + 1) LININTERP = y1 + (y2 - y1) * (x - x1) / (x2 - x1) End Function 

在这里输入图像说明

道德故事:不要相信你在互联网上find的所有东西。