Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Global variables cleared by forms?

Hi all,

I am designing a project set up using a dozen or so public subs. They
are very simple, and i am using global variables (integers) to keep
track of usage statistics for each procedure.

Normally, each procedure runs fine, and adds 1 to its own usage stat.
I'm then outputting these to a text file at the end of each session,
and inputting them at the start of the next (no problems here).

Two of the procedures call (show) forms. My problem is that when the
forms close (either "unload" or just clicking "X"), the code returns
to "End Sub" in the calling procedure, and THEN....all my global
variables get cleared.

(I've checked the exact point by monitoring them line by line in the
Immediate window.)

But this doesnt happen with any of the other subs, only the two that
call forms.

Clearly im not getting something about global variables and/or forms.
Can anyone help? Also, if there isnt a simple explanation, possible
workarounds much appreciated.

Cheers

Stu
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Global variables cleared by forms?

Stu,

Firstly, are all of your code modules got he statement 'Option Explicit' at
the start? If not, add it.

Secondly, is the variable outside of any macros?

Thirdly, where is the global declared? In the form? If so the variable in
the calling module should use Userform1.varname instead of varname,
otherwise we are using different variables (Option Explicit will trap this).

It does work, I just tried it again, and it even stays fro subsequent
re-launches of the form.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Stu" wrote in message
m...
Hi all,

I am designing a project set up using a dozen or so public subs. They
are very simple, and i am using global variables (integers) to keep
track of usage statistics for each procedure.

Normally, each procedure runs fine, and adds 1 to its own usage stat.
I'm then outputting these to a text file at the end of each session,
and inputting them at the start of the next (no problems here).

Two of the procedures call (show) forms. My problem is that when the
forms close (either "unload" or just clicking "X"), the code returns
to "End Sub" in the calling procedure, and THEN....all my global
variables get cleared.

(I've checked the exact point by monitoring them line by line in the
Immediate window.)

But this doesnt happen with any of the other subs, only the two that
call forms.

Clearly im not getting something about global variables and/or forms.
Can anyone help? Also, if there isnt a simple explanation, possible
workarounds much appreciated.

Cheers

Stu



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Global variables cleared by forms?

Thanks Bob.

To answer your qs:

The module and both forms im using have Option Explicit.

There is one module, and two forms (each called by one proc). The
public variables are declared in Declarations in the module. They are
updated within the procedures, but not within either form.

I'm not sure what you mean by "outside" the macros? The public vars
are being used very simply to track each usage of each procedure, so
they are only used once per proc, as in "intUsageStat = intUsageStat +
1" etc.

I've just noticed that it is only one of the procedures calling the
forms that seems to clear the variables. The only thing that appears
to be different about this one is that the form uses a private
function. Do functions clear public variables?!

Thanks for your help

Stu


"Bob Phillips" wrote in message ...
Stu,

Firstly, are all of your code modules got he statement 'Option Explicit' at
the start? If not, add it.

Secondly, is the variable outside of any macros?

Thirdly, where is the global declared? In the form? If so the variable in
the calling module should use Userform1.varname instead of varname,
otherwise we are using different variables (Option Explicit will trap this).

It does work, I just tried it again, and it even stays fro subsequent
re-launches of the form.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Stu" wrote in message
m...
Hi all,

I am designing a project set up using a dozen or so public subs. They
are very simple, and i am using global variables (integers) to keep
track of usage statistics for each procedure.

Normally, each procedure runs fine, and adds 1 to its own usage stat.
I'm then outputting these to a text file at the end of each session,
and inputting them at the start of the next (no problems here).

Two of the procedures call (show) forms. My problem is that when the
forms close (either "unload" or just clicking "X"), the code returns
to "End Sub" in the calling procedure, and THEN....all my global
variables get cleared.

(I've checked the exact point by monitoring them line by line in the
Immediate window.)

But this doesnt happen with any of the other subs, only the two that
call forms.

Clearly im not getting something about global variables and/or forms.
Can anyone help? Also, if there isnt a simple explanation, possible
workarounds much appreciated.

Cheers

Stu

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Global variables cleared by forms?

Stu,

By outside the macros, I was meaning in the Declarations section.

Public variables are active as long as the project (workbook) is open, so
although I don't know what you are getting at when you say '... do functions
clear public variables ... ' would seem to be an emphatic no!

Post your code, making it clear what is in each module/form.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Stu" wrote in message
m...
Thanks Bob.

To answer your qs:

The module and both forms im using have Option Explicit.

There is one module, and two forms (each called by one proc). The
public variables are declared in Declarations in the module. They are
updated within the procedures, but not within either form.

I'm not sure what you mean by "outside" the macros? The public vars
are being used very simply to track each usage of each procedure, so
they are only used once per proc, as in "intUsageStat = intUsageStat +
1" etc.

I've just noticed that it is only one of the procedures calling the
forms that seems to clear the variables. The only thing that appears
to be different about this one is that the form uses a private
function. Do functions clear public variables?!

Thanks for your help

Stu




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Global variables cleared by forms?

Apologies...both the procedures calling forms are clearing the variables.

"Bob Phillips" wrote in message ...
Stu,

Firstly, are all of your code modules got he statement 'Option Explicit' at
the start? If not, add it.

Secondly, is the variable outside of any macros?

Thirdly, where is the global declared? In the form? If so the variable in
the calling module should use Userform1.varname instead of varname,
otherwise we are using different variables (Option Explicit will trap this).

It does work, I just tried it again, and it even stays fro subsequent
re-launches of the form.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Stu" wrote in message
m...
Hi all,

I am designing a project set up using a dozen or so public subs. They
are very simple, and i am using global variables (integers) to keep
track of usage statistics for each procedure.

Normally, each procedure runs fine, and adds 1 to its own usage stat.
I'm then outputting these to a text file at the end of each session,
and inputting them at the start of the next (no problems here).

Two of the procedures call (show) forms. My problem is that when the
forms close (either "unload" or just clicking "X"), the code returns
to "End Sub" in the calling procedure, and THEN....all my global
variables get cleared.

(I've checked the exact point by monitoring them line by line in the
Immediate window.)

But this doesnt happen with any of the other subs, only the two that
call forms.

Clearly im not getting something about global variables and/or forms.
Can anyone help? Also, if there isnt a simple explanation, possible
workarounds much appreciated.

Cheers

Stu



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
I have a file that is 4mb. Cleared all formating...still big JDH Excel Worksheet Functions 4 January 28th 10 07:20 PM
Password cleared in clean up Fiona[_2_] Excel Discussion (Misc queries) 0 January 12th 09 05:10 AM
Global Variables using User Forms Steve[_53_] Excel Programming 4 January 31st 04 02:38 AM
Global Variables Not Accessible John Baker Excel Programming 5 November 28th 03 01:23 AM
Declaring Global Variables skmr3 Excel Programming 1 July 14th 03 05:54 AM


All times are GMT +1. The time now is 06:30 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"