Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Inserting and retaining an input variable | Excel Discussion (Misc queries) | |||
merge two cel into ONE retaining the two values | Excel Discussion (Misc queries) | |||
VBA variables retaining their values | Excel Discussion (Misc queries) | |||
Retaining Values in Userform when called a second time | Excel Programming | |||
variable retaining value between subroutins question | Excel Programming |