Variables...the passing of.

L

Les

I have discovered I can call a previously written event code from a new one
simply by repeating the name of that routine. Obvious, I guess, for a
programmer, but nice to discover for a beginner.
My question is: I dim a variable in the first routine, then call the
second. But, the VB error box always appears with:

"Compile error: Variable not defined."

If I dim the variable in the second routine, it loses the value it was
assigned from the first routine.
How do I pass the value of a variable from one event code (routine) to
another? Or can variables only be recognized within the same routine?

Les
 
C

chas

Hi Les,

You can declare the variable at the module level and it
then becomes available to the whole application, as in:

Public MyVar As String 'or whatever

you can then use this anywhere.

Variables have scope and lifetime, that is they retain
their values depending on where/how they are declared.

Search VBA help for 'lifetime' or 'scope' for some more
info.

hth

chas
 
C

chas

Hi Les,

I replied earlier but my post appears to have gone astray,
apologies if it appears and gives a duplicate reply.

You can define a variable in the declarations section of a
module (outside of any routines) that can have global
scope. For example:

Public MyVar As String 'or whatever datatype

this variable is then usable from any routine.

For more information search VBA help for 'lifetime'
or 'scope' (without the quotes.

hth

chas
 

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