简体   繁体   English

在Excel中使用自定义功能导致#Value错误

[英]#Value error using custom function in Excel

I'm getting a #VALUE error when I try to call my custom function. 尝试调用自定义函数时出现#VALUE错误。 All it is supposed to do is a bit of math. 它应该做的只是一点数学。 Does anyone see what could be wrong here? 有人看到这里有什么问题吗?

I copied this one off the internet: 我从互联网上复制了这个:

Source: 资源:

http://www.engineerexcel.com/linear-interpolation-vba-function-in-excel/ http://www.engineerexcel.com/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

This is a simplified version I made thinking that the worksheet function calls may be causing the error: 这是我考虑到工作表函数调用可能导致错误的简化版本:

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

my test data in an unrelated workbook: (All formatted as "General") 我的测试数据在一个不相关的工作簿中:(全部格式化为“常规”)

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

Just plugging the actual math into a cell works as expected. 只需将实际的数学运算插入单元格即可。 Calling the function throws the #VALUE error. 调用该函数将引发#VALUE错误。

My function is saved in a module in a workbook that I have saved and added to Excel as an Add-In. 我的功能保存在工作簿的模块中,该模块已保存并作为外接程序添加到Excel。

My sampling of your formula and data threw an error on the hyphens not being interpreted as 'minus signs'. 我对公式和数据的采样导致连字符出现错误,连字符没有被解释为“减号”。 In fact, they come up as unicode 8211. Retyping them, declaring the vars as variants and removing the ...WorksheetFunction... fixed the problem. 实际上,它们以unicode 8211的形式出现。重新键入它们的名称,将vars声明为变体,然后删除...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

在此处输入图片说明

Moral of the story: Don't trust everything you find on the internet. 故事的寓意:不要相信在互联网上找到的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM