Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Life of a variable

Except for Public variables declared at the top of general modules, a
variables scope is limited to the procedure in which it is declared. You
can pass a variable in the argument list of procedure (either byref or
byval). If byval, think of it as being local to that procedure (it is just
initialized by the calling procedure). If byref, then the variable is
visible to both procedures until the called procedure returns control back
to the calling procedure. Once code stops running, the only variables
retaining there values would be public variables - think of them as being
"local" to the workbook/project - so as long as that is running, they retain
their values unless you take action (such as reset) to change them.

The only generalized command that will clear all variables is the END
statement, but it also halts code immediately.

--
Regards,
Tom Ogilvy

"Mike Fogleman" wrote in message
...
I have a huge amount of code, over 80 subs, spread over 4 general modules,
with a userform and workbook open & close. Most variables are keeping

their
last value when the routine has ended. When I run the routine again, on
different data, the results are not correct. Re-running the routine
immediately on this data works.

2 questions: VB help says a variable will retain its' scope when calling

one
sub from another. I have found that if the called sub is in another

module,
scope is lost. Is this normal? Declaring the variable Public in the
originating module works, so I have a lot of Public variables to keep the
code running as it criss-crosses the 4 modules.

Second, is there a global way to reset the variables between runs, like

the
Reset button on the VB Editor?
TIA, Mike




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
!very important for your life... [email protected] Excel Worksheet Functions 0 March 27th 09 03:45 PM
life contingencies sylphide Excel Worksheet Functions 1 February 2nd 06 01:51 PM
asset life otrain Excel Discussion (Misc queries) 1 November 19th 05 12:02 AM
Money for life mike Excel Discussion (Misc queries) 0 February 25th 05 10:28 AM
Variable life morph Excel Programming 1 December 3rd 03 10:57 PM


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

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

About Us

"It's about Microsoft Excel"