运行调用该模块的程序后,访问另一个模块中的模块变量?

Abd*_*yas 3 excel vba

我有一个 Excel 文件,其中有 2 个按钮,可访问两个不同的模块。运行调用该模块的程序后,我们可以在另一个模块中访问该模块的变量吗?

我的模块看起来像这样 在此输入图像描述

第一个模块..

Public Sub Directory_Path()
Dim Directory As String
    Directory = InputBox("Enter the Directory path that contains folders ""This Quarter"",""Last Quarter"",""Second_Last_Quarter"".")
    If Right(Directory, 1) = "\" Then
    Directory = Left(Directory, Len(Directory) - 1)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

我使用 Public Sub Directory_Path() 调用第二个模块中的第一个模块。我希望第一个模块中的目录变量用作第二个模块中的变量...

bar*_*owc 5

在第一个模块中 - 在任何子/函数之外的模块顶部将目录声明为公共。现在该项目中的每个模块都可以使用它:

Public Directory As String

Sub Directory_Path()

    Directory = InputBox("Enter the Directory path that contains folders ""This Quarter"",""Last Quarter"",""Second_Last_Quarter"".")
    If Right(Directory, 1) = "\" Then
    Directory = Left(Directory, Len(Directory) - 1)
    End If

End Sub
Run Code Online (Sandbox Code Playgroud)

Directory在第二个模块中,只需在需要的地方使用该名称即可。例子:

MsgBox "The directory path is " & Directory
Run Code Online (Sandbox Code Playgroud)