How do I make a function in vba?

H

hanski

hi.

I have Private sub name1() and Private sub name2() in my vba.

I have Dim X as interger and I give value to X in Private sub name1().

I would like to use X also in Private sub name2(). Do I have to make a
function or how can I use this X-value I gave in Private sub name1()
also in Private sub name2()?

t hanski
 
W

Wolfgang Kais

Hello "hanski".

"hanski"wrote:
I have Private sub name1() and Private sub name2() in my vba.

I have Dim X as interger and I give value to X in Private sub name1().

I would like to use X also in Private sub name2(). Do I have to make
a function or how can I use this X-value I gave in Private sub name1()
also in Private sub name2()?

The problem seems to be the "scope" of the variable x. If you declared
x inside of name1, name2 can't access that variable. If both procedures
were defined inside the same module, you could declare x at the module
level (at the top of the module, beneath the Option line(s) and before
any sub or function. You can use "Dim" or "Private" to declare x.
If name1 and name2 were defined in different modules, you will have to
declare x as a global variable in a standard module, using
Public x As Integer
 
Top