View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default VBA variables retaining their values

Global variables will hold their values for the duration that the spreadsheet
is open with a few exceptions.

If you stop the execution in the VBE then the global variables will be
cleared.
If you use the stand alone line of code "End" then global variables will be
cleared.
If your code ends due to an unhandled error then global variables will be
cleared.

All of that being said you want to minimize your use of global variables as
they can be a nightmare to debug if they end up by holding values that are
incorrect. The problem is that since the variable is global it can be very
difficult to trace which sub or function changed the variable to the
incorrect value.

Another way to make a variable ho0ld it's value is to declare it within a
procedure as Static.
--
HTH...

Jim Thomlinson


"GeorgeJ" wrote:

I have a VBA application which uses global variables defined at the top of
the Module. I have found that if I leave the applpication and go back to the
regular mode of usage of Excel, then if I rerun the app the globals seem to
retain the vaules they were given the last time I ran it. Can I count on
this? Is there any way to ensure that this will always be the case?



--
-regards