![](/img/trans.png)
[英]Excel VBA - Custom Function; #VALUE error; VLOOKUP on different worksheet
[英]Create a custom worksheet function in Excel VBA
我有一个微弱的记忆,能够使用VBA函数来计算Excel中的值,像这样(作为单元格公式):
=MyCustomFunction(A3)
可以这样做吗?
编辑:
这是我的VBA功能签名:
Public Function MyCustomFunction(str As String) As String
该函数位于ThisWorkbook
模块中。 如果我尝试在工作表中使用它,如上所示,我得到#NAME?
错误。
解决方案(谢谢,代码):定义ThisWorkbook
模块时无法访问该功能。 它必须位于“正确”模块中,该模块已手动添加到工作簿中。
是的,它可以。 您只需在模块中定义VBA函数。 有关示例的详细介绍,请参阅http://www.vertex42.com/ExcelArticles/user-defined-functions.html 。
这是一个简单的例子:
Option Explicit Function MyCustomFunction(input) MyCustomFunction = 42 + input End Function
A1: 2 A2: =MyCustomFunction(A1)
输入一词需要替换,因为它是一个基本关键字。 请尝试使用num。 您还可以通过指定类型(例如变体)来进一步发展。
Function MyCustomFunction(num As Variant)
MyCustomFunction = 42 + num
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.