retaining variable values across modules

C

c1802362

If I want to keep a variable in memory through several macros, I can
retain the variable value if I dim it above the subroutines in the
module

So, in module 1:

Dim String1 as String

Sub test1()
string1 = " test"
end sub

sub test2()
msgbox string1
end sub

the value of string1 is retained into sub test2, test 3, etc

However, all my attempts to retain the value of string1 from module 1
to another module nulls the value of string1.

How do I maintain the value of a variable across modules, not only
within a module?

Art
 
O

OssieMac

What I do to work around this problem is to place all the subs that require
the variable in 1 module and simply call the subs from the various other
modules.

Another way is to save the values to cells on a worksheet that can be hidden.
 
F

Fred

If you declare String1 using

Public String1 as String

then String1 will be available to all modules.

Regards,
Fred
 
T

Tim Zych

You could use a global variable rather than a module level variable as you
are doing.

Public String1 as String

But there is usually a better way to pass data than by using global
variables.

Check out http://en.wikipedia.org/wiki/Global_variable which has a tidy
summary of when to use them, and when to avoid them. Or google "why avoid
global variables"
 

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