简体   繁体   English

从电子表格在另一个工作簿中调用VBA函数

[英]Call VBA function in another workbook from spreadsheet

Here's the function definition: 这是函数定义:

Public Function StockQuote(strSymbol As String) As Double 

It's stored in a module in an already-loaded worksheet ("My Macros.xlsm", in my Startup folder). 它存储在已加载的工作表的模块中(“我的启动”文件夹中的“ My Macros.xlsm”)。

I want to call it from another workbook, as a cell reference: 我想从另一个工作簿中调用它作为单元格引用:

Workbook1.xlsm cell A1: Workbook1.xlsm单元格A1:

=StockQuote("AAPL")

But all i get are NAME errors. 但是我得到的都是NAME错误。

=My Macros.xlsm!StockQuote("AAPL") =我的Macros.xlsm!StockQuote(“ AAPL”)

='C:\\SomeFolder\\My Macros.xlsm'!StockQuote("AAPL") ='C:\\ SomeFolder \\ My Macros.xlsm'!StockQuote(“ AAPL”)

Work either. 要么工作。

But in order to work you shoud open the My Macros.xlsm workbook after Workbook1.xlsm, from the excel window of Workbook1.xlsm 但是为了工作,您应该在Workbook1.xlsm之后从Workbook1.xlsm的excel窗口中打开My Macros.xlsm工作簿。

尝试将包含该功能的XLSM工作簿另存为XLAM:打开XLAM时,其他工作簿应该能够看到您的UDF。

How about something like this. 这样的事情怎么样。

It should run when the worksheet is activated and put the return value of your function in cell A1. 它应在工作表激活时运行,并将函数的返回值放在单元格A1中。

Private Sub Worksheet_Activate()
    Range("A1") = StockQuote("AAPL")
End Sub

You may need to structure it differently if the current worksheet can't see your function 如果当前工作表看不到您的功能,则可能需要以不同的方式构造它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM