0

I tried looking for the answer for this question, but go no where. So I understand if I declare a variable outside a sub/function, it's accessible to all the subs/functions with in that module. What about other modules? Do I have to declare it? Or can I use it without declaring it?

For example, I have a textstream object that's used for logging in multiple modules. Basically what I want do is to initialize them once and all the functions in my modules can use the same objTF object to write to a log file. These objects are initialized in a commandbutton_click event. But where do I declare them?

Dim objFSO As FileSystemObject
Dim objTF As TextStream

Do I have to put them at the beginning in all of the modules? Or only one of the modules is enough? What is the correct way to do it?

Thanks!!

user511792
  • 489
  • 1
  • 13
  • 23

1 Answers1

3

try this (outside of a sub or function):

Public objFSO As FileSystemObject
Public objTF As TextStream

that should declare them globally so that they are accessible from any module in the project.

edit:

there's a good explanation of vba variable scoping in the accepted answer to this question.

Community
  • 1
  • 1
nullrevolution
  • 3,937
  • 1
  • 18
  • 20