EXCEL VBA用户定义函数 - 当我在定义它的工作簿中使用它时,为什么我会收到错误“函数未定义”?

问题描述 投票:0回答:2

在同一个工作簿中,我定义了函数:

Public Function toto(val As Integer) As Integer
    toto = val * 2
End Function

这是编辑器视图:

enter image description here

在工作表中,在单元格中,我使用以下公式:

enter image description here

但结果是(翻译:#NAME?):

enter image description here

此外,在从Excel打开的VBA exec窗口中,我在尝试执行时收到以下错误:

toto(2)

enter image description here

翻译:编译错误:未定义子或函数

我错过了什么?

excel vba excel-formula
2个回答
3
投票

扩展@ Jo.lass和@ vincent-g所说的内容,您需要将代码放在模块中,而不是工作表或工作簿代码中。看这里:

enter image description here


2
投票

不要在VBE的工作表中编写函数,而是尝试在新模块中编写它。至少对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.