当我尝试使用我在 Excel 电子表格中定义的 VBA 函数时,我得到“这不是一个有效的函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/893804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When I try to use a VBA function that I've defined in an Excel spreadsheet, I get "That is not a valid function"
提问by Matt
I defined a function in VBA as follows
我在 VBA 中定义了一个函数如下
Public Function Distance(X, Y) as Double
Then in a cell I try to use the function. I type "@Dis" and get the drop down menu, select the function and then enter the parameters ending up with @Distance(A1, A2) in the cell.
然后在一个单元格中我尝试使用该功能。我输入“@Dis”并获取下拉菜单,选择函数,然后在单元格中输入以@Distance(A1, A2) 结尾的参数。
When I press Enter I get the Error "That function is not valid".
当我按 Enter 时,出现错误“该功能无效”。
I enabled macros throughout the system, tried saving it as the old format and as the macro enabled workbook format to no avail.
我在整个系统中启用了宏,尝试将其保存为旧格式和启用宏的工作簿格式,但无济于事。
What's wrong with my usage of this function?
我使用这个函数有什么问题?
回答by e.James
Try using:
尝试使用:
=Distance(A1, A2)
Instead of
代替
@Distance(A1, A2)
I've never seen @
as the correct character to call a function in Excel.
我从未认为@
在 Excel 中调用函数是正确的字符。
I tried the following in Excel, and it works like a charm:
我在 Excel 中尝试了以下操作,它的作用就像一个魅力:
In Module1:
在模块 1 中:
Public Function Distance(X as Double, Y as Double) as Double
Distance = 10
End Function
In a cell:
在一个单元格中:
=Distance(A1, A2)
Which produces the result:
结果如下:
10
10
as expected.
正如预期的那样。
回答by Mark Biek
You'll also need to make sure that the VBA code for your function is in a Moduleand not in the code area of the Worksheet.
您还需要确保函数的 VBA 代码在模块中,而不是在工作表的代码区域中。
回答by dgoodman1
Do not use reserved words as the names of your functions. I tried one name Parse() and it kicked it back until I renamed it ParseString().
不要使用保留字作为函数的名称。我尝试了一个名字 Parse(),它把它踢回去,直到我将它重命名为 ParseString()。