如何访问另一个Excel工作簿中定义的公共常量?
安装程序
工作簿.xls
它有一些VBA代码,可以读取文件名--“其他工作簿.xls在这种情况下——从单元格“A1”。然后,代码应该读取另一个工作簿中定义的常量,并将其写入单元格“A2”。下面是我的代码:
Sub GetValueFromOtherWorkbook()
otherWorkbook = Range("A1").Value ' Value = "OtherWorkbook.xls"
Dim returnedValue As String
' Idea 1: Doesn't work
Set returnedValue = OtherWorkbook.xls!Module1.MY_CONSTANT
' Idea 2: Doesn't work either
Set returnedValue = Application.Run(otherWorkbook & "!Module1.MY_CONSTANT")
Range("A2").Value = returnedValue ' MY_CONSTANT = "testValue"
End Sub
其他工作簿.xls
它有一个名为Module1的VBA模块。
Module1定义了一个公共常量。代码如下所示:
Public Const MY_CONSTANT As String = "testValue"
代码来自我的工作簿.xls如果不运行,则返回以下错误
需要对象
即使阅读了帮助文档,我也不知道该怎么处理它。在另一个上下文中,我使用以下代码调用了另一个工作簿中的例程:
otherWorkbookName = "OtherWorkbook.xls"
Application.Run (otherWorkbookName & "!Module1.SomeRoutine")
你知道我下一步能做什么吗?