Variables available to all subs in one module?

E

Ed

For some reason, my brain's not working this morning! I'm writing a whole
routine as separate subs all within one module. I'd like to have several
variables available to a couple of the different subs. Does declaring them
all between the Option Explicit and the first Sub line make them available
to the whole module?

Ed
 
A

and

[...] Does declaring them
all between the Option Explicit and the first Sub line make them available
to the whole module?

I do that with a "Public" statement, right after Option Explicit, yes.

Public MyVar as Variant
 
E

Elix

I thought that defining the variable(s) as PUBLIC up at
the top of the module would do it. But I'm not sure.
 
E

Ed

Okay - the coffee's kicking in and it's coming back slowly ... thank you!

Ed

and said:
[...] Does declaring them
all between the Option Explicit and the first Sub line make them available
to the whole module?

I do that with a "Public" statement, right after Option Explicit, yes.

Public MyVar as Variant
 
P

Peter Hewett

Hi Ed

If you want the variable accessible by all procedures in the module the variable
is declared in declare it as Private (not Public).

If you want the variable accessible by all procedure in your project (any module
in your project) then declare it as Public. However, declaring a variable as
Public gives code outside of your project access to that variable by default.
This is an issue if you develop Add-Ins. If you want a Public variable
accessible to just your project add an "Option Private Module" statement on the
line after the "Option Explicit" statement of the module you declare your
variable in.

Incorrect declaration of variable scope leads to all kinds of inadvertent bugs
and is sloppy programming practice. So don't pick up the habit in the first
place!

HTH + Cheers - Peter


Okay - the coffee's kicking in and it's coming back slowly ... thank you!

Ed

and said:
[...] Does declaring them
all between the Option Explicit and the first Sub line make them available
to the whole module?

I do that with a "Public" statement, right after Option Explicit, yes.

Public MyVar as Variant
 
E

Ed

Got it - "Private" it is! Thanks, Peter.

Ed

Peter Hewett said:
Hi Ed

If you want the variable accessible by all procedures in the module the variable
is declared in declare it as Private (not Public).

If you want the variable accessible by all procedure in your project (any module
in your project) then declare it as Public. However, declaring a variable as
Public gives code outside of your project access to that variable by default.
This is an issue if you develop Add-Ins. If you want a Public variable
accessible to just your project add an "Option Private Module" statement on the
line after the "Option Explicit" statement of the module you declare your
variable in.

Incorrect declaration of variable scope leads to all kinds of inadvertent bugs
and is sloppy programming practice. So don't pick up the habit in the first
place!

HTH + Cheers - Peter


Okay - the coffee's kicking in and it's coming back slowly ... thank you!

Ed

and said:
[...] Does declaring them
all between the Option Explicit and the first Sub line make them available
to the whole module?

I do that with a "Public" statement, right after Option Explicit, yes.

Public MyVar as Variant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top