vba 从excel单元格调用excel工作表函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4391229/
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
calling excel worksheet function from excel cell
提问by tbischel
I have a set of user defined vba functions that sit in an excel module, which are then called from an excel spreadsheet... everything has worked fine at this point.
我有一组用户定义的 vba 函数,它们位于 excel 模块中,然后从 excel 电子表格中调用这些函数……此时一切正常。
I've been asked to move the vba from the module to the worksheet's code page. When I did this, I've found I can't call any of the functions from cells on the worksheet... the names simply don't show as existing. Is there a way to call worksheet functions from an excel cell? Also, is there any problem calling a worksheet function from a user defined function in another module or worksheet code behind?
我被要求将 vba 从模块移动到工作表的代码页。当我这样做时,我发现我无法从工作表上的单元格中调用任何函数......名称根本不显示为存在。有没有办法从excel单元格调用工作表函数?另外,从另一个模块中的用户定义函数或后面的工作表代码调用工作表函数是否有任何问题?
EDIT:
编辑:
I've found if I call by the sheetname.functionname, it throws an error message that includes "The name conflicts with an Excel built-in name or the name of another object in the workbook"... where if I use sheetname.anythingelse it just resolves to #NAME?
我发现如果我通过 sheetname.functionname 调用,它会抛出一条错误消息,其中包括“名称与 Excel 内置名称或工作簿中另一个对象的名称冲突”......如果我使用 sheetname。还有什么它只是解析为#NAME?
Does this mean excel worksheet functions cannot be called from a sheet?
这是否意味着不能从工作表调用 Excel 工作表函数?
回答by Joel Spolsky
No. Functions in the worksheet object can't be called from the sheet as user-defined functions.
不能。工作表对象中的函数不能作为用户定义函数从工作表中调用。
The Worksheet object is intended for responding to events that happen on a worksheet. You can't put user-defined functions there. User defined functions have to live in a Module.
Worksheet 对象用于响应工作表上发生的事件。你不能把用户定义的函数放在那里。用户定义的函数必须存在于模块中。
If your user-defined function does live in a module, you won't have any problem calling it from code anywhere else... including in the worksheet "code-behind".
如果您的用户定义函数确实存在于模块中,那么您从其他任何地方的代码调用它都不会有任何问题……包括在“代码隐藏”工作表中。
回答by CaBieberach
You have to put the code in a standard module. Check this link.
您必须将代码放在标准模块中。检查此链接。