ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   retaining variable values across modules (https://www.excelbanter.com/excel-programming/425297-retaining-variable-values-across-modules.html)

c1802362[_2_]

retaining variable values across modules
 
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

OssieMac

retaining variable values across modules
 
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.

--
Regards,

OssieMac


"c1802362" wrote:

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


fred

retaining variable values across modules
 
If you declare String1 using

Public String1 as String

then String1 will be available to all modules.

Regards,
Fred


"c1802362" wrote in message
...
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




Tim Zych

retaining variable values across modules
 
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"


--
Tim Zych
http://www.higherdata.com
Workbook Compare - Excel data comparison tool
Free and Pro versions


"c1802362" wrote in message
...
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





All times are GMT +1. The time now is 11:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com