如何从Excel单元格(2010)调用VBA函数? [英] How to call VBA function from Excel cells (2010)?
本文介绍了如何从Excel单元格(2010)调用VBA函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用VBA的工作簿中定义了一些函数,然后希望能够在单元格公式中使用它们-但是Excel无法识别该函数.我刚得到#NAME?
I defined a few functions in a workbook using VBA, and then expected to be able to use them in a cell formula - but Excel does not recognise the function. I just get #NAME?
尝试:
- 意识到我已经创建了XSLX文件,所以将其转换为XSLM文件.没用.
- 从函数声明中删除了所有类型.没用.
- 将该功能移动到工作表VBA模块中.没用.
- 在声明中添加了公共".没用.
我想念什么?
这也不是聪明的代码:
Function Square2(AnyNumber)
'return the square of any integer
Square2 = AnyNumber * AnyNumber
End Function
推荐答案
答案
将功能放在"ThisWorkbook"区域中会导致#NAME?
问题.创建一个新模块(右键单击VBAProject文件夹,插入,新建模块)
并将功能放在那里.
Answer
Putting the function in the "ThisWorkbook" area can cause the #NAME?
problem. Create a new Module (Right Click on the VBAProject Folder, Insert, New Module)
and put the function there instead.
- 打开VBA编辑器(在Windows上为
Alt + F11
在Mac上为Fn + Option + F11
) - 右键单击 VBAProject
- 选择插入>>模块
-
在
Module1
内创建一个Public
函数,例如:
- Open the VBA Editor (
Alt + F11
on Windows /Fn + Option + F11
on a Mac) - Right-click VBAProject
- Select Insert >> Module
Create a
Public
function insideModule1
, for example:
Public Function findArea(ByVal width as Double, _
ByVal height as Double) As Double
' Return the area
findArea = width * height
End Function
像其他任何函数一样,从单元格中调用它:=findArea(B12,C12)
这篇关于如何从Excel单元格(2010)调用VBA函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文