Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting and retaining an input variable ARGT Excel Discussion (Misc queries) 4 June 26th 08 11:42 PM
merge two cel into ONE retaining the two values victork Excel Discussion (Misc queries) 2 February 20th 08 10:13 PM
VBA variables retaining their values GeorgeJ Excel Discussion (Misc queries) 2 August 17th 07 02:19 AM
Retaining Values in Userform when called a second time ExcelMonkey Excel Programming 2 May 3rd 07 03:55 PM
variable retaining value between subroutins question Barmaley Excel Programming 2 September 27th 03 03:50 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"