如何访问另一个Excel工作簿中定义的常量?

发布于 2024-09-24 15:39:47 字数 1274 浏览 6 评论 0原文

问题

如何访问另一个 Excel 工作簿中定义的公共常量?

安装

Workbook.xls

我的 VBA 代码在名为“MyWorkbook.xls”的当前 Excel 工作簿中运行。 它包含一些 VBA 代码,用于从单元格“A1”读取文件名(本例中为“OtherWorkbook.xls”)。然后,该代码应读取该其他工作簿中定义的常量并将其写入单元格“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

OtherWorkbook.xls

第二个 Excel 工作簿名为“OtherWorkbook.xls”,如上所述。 它有一个名为 Module1 的 VBA 模块。 Module1 定义了一个公共常量。代码如下所示:

Public Const MY_CONSTANT As String = "testValue"

问题

MyWorkbook.xls 中的代码无法运行,它返回以下错误

运行时错误“424”

需要对象

我什至不知道该怎么处理它阅读帮助文档后。在另一个上下文中,我设法使用以下代码调用另一个工作簿中的例程:

otherWorkbookName = "OtherWorkbook.xls"
Application.Run (otherWorkbookName & "!Module1.SomeRoutine")

因此,调用例程有效,但访问公共常量则无效。

有什么想法我接下来可以尝试吗?

Question

How do I access a public constant defined in another Excel workbook?

Setup

Workbook.xls

My VBA code runs in the current Excel workbook called "MyWorkbook.xls".
It has some VBA code in it which reads a file name --"OtherWorkbook.xls in this case-- from cell "A1". Then, the code should read a constant defined in that other workbook and write it to cell "A2". Here is what my code looks like:

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

OtherWorkbook.xls

The second Excel workbook is called "OtherWorkbook.xls" as mentioned above.
It has a VBA module called Module1.
Module1 defines a public constant. The code looks like this:

Public Const MY_CONSTANT As String = "testValue"

Problem

The code from MyWorkbook.xls does not run, it returns the following error

Run-time error '424'

Object required

I don't quite know what to do with it even after reading the help documentation. In another context, I managed to call a routine in another workbook with this code:

otherWorkbookName = "OtherWorkbook.xls"
Application.Run (otherWorkbookName & "!Module1.SomeRoutine")

So, calling a routine works, but accessing a public constant does not.

Any ideas what I could try next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-10-01 15:39:47

代码模块不是可以像工作表一样访问的对象。

您可以访问和操作它,但为此,您需要引用 Microsoft Visual Basic for Applications Extensibility 库并允许访问 VBA 项目(工具 - 选项 - 安全性 - 宏安全性 - 受信任)发布者 - 信任对 VBA 项目的访问),之后您可以使用第二个工作簿的 Workbook.VBProject 属性。

你不需要这样做。相反,您可以创建对其他工作簿的引用:在 VBA IDE 中,转到“工具”-“引用”,单击“浏览”并找到包含常量的工作簿。

如果这不好,您可以在该工作簿中创建一个仅返回常量值的方法,并按照您所演示的方式调用它。

A code module is not an object that can be accessed like a worksheet.

You can access and manipulate it, but in order to do that you need to reference the Microsoft Visual Basic for Applications Extensibility library and allow access to the VBA project (Tools - Options - Security - Macro security - Trusted publishers - Trust access to VBA project), after which you can use the Workbook.VBProject property of the second workbook.

You don't need to do this. Instead you can create a reference to the other workbook: in VBA IDE, go Tools - References, click Browse and locate your workbook with the constant.

If that's no good, you can create a method in that workbook that only returns the value of the constant, and call it as you have demonstrated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文