vba 无效的外部程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6714343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:40:09  来源:igfitidea点击:

Invalid outside procedure

excel-vbauserformvbaexcel

提问by user793468

I want to store a path in string "path" which would be available for the whole workbook.

我想在字符串“path”中存储一个路径,该路径可用于整个工作簿。

When I do it the following way, I get an error: "invalid outside procedure"

当我按以下方式执行时,出现错误:“外部程序无效”

Here is how I am trying to declare the path in (General) (Declaration)

这是我尝试在 (General) (Declaration) 中声明路径的方式

Option Explicit
Dim path As String

 path = "\sharedDrive\TestFolder\Test_PDF\"

I get the error at:

我在以下位置收到错误:

"\sharedDrive\TestFolder\Test_PDF\"

回答by Jacob

Put the variable in a module instead, like this:

将变量放在模块中,如下所示:

Public path As String

Then, in your Workbook_Open routine:

然后,在您的 Workbook_Open 例程中:

Private Sub Workbook_Open()
    yourModule.path = "\sharedDrive\TestFolder\Test_PDF\"
End Sub

To access it anywhere, use yourModule.path.

要在任何地方访问它,请使用yourModule.path.