Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a variable of a module in another module after running the program which calls that module?

Tags:

excel

vba

I have an excel file with 2 buttons which access two different modules. Can we access a variable of a module in another module after running the program which calls that module?

My modules look like this enter image description here

1st module..

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

I called the the first module in 2nd module using Public Sub Directory_Path() . I want Directory variable in first module to be used as a variable in 2nd module...

like image 488
Abdul Shiyas Avatar asked Nov 19 '25 14:11

Abdul Shiyas


1 Answers

In 1st module - declare Directory as Public at top of module outside of any Sub/Function. It's now available to every module in this project:

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

In 2nd module, just use the name Directory wherever you need it. Example:

MsgBox "The directory path is " & Directory
like image 112
barrowc Avatar answered Nov 22 '25 03:11

barrowc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!