A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Defining public/global variables in more than one place



 
 
Thread Tools Display Modes
  #1  
Old July 13th 12, 08:41 AM posted to microsoft.public.excel.programming
stainless[_2_]
external usenet poster
 
Posts: 5
Default Defining public/global variables in more than one place

Using Excel 2003 (hopefully will persuade my company to allow us to upgrade our excel reports to 2010 at some point!)

I am trying to standardise the code in our executables to make coding/maintenance easier and have come up with the following idea:

1. Our Excel reports have a simple Auto_Open module that will be the first piece of code in every report we have and this has 1 public variable plus one actual command to call another module sub called Main_Processing. This other module will contain the report relevant logic.

2. This public variable defined in the Auto_Open module is a string defining the environment the code runs in such as Test, Development and thus can be altered simply for each environment. Lower level code uses this to pick up data files from the correct place for that environment.

3. This is the part my question relates to. Can the Main_Processing module also have public variables that are used in lower level sub routines and functions? So far, I have only defined global variables in one place at the top level, so the question is really about scope of lower level variables.

Cheers
Ads
  #2  
Old July 13th 12, 10:28 AM posted to microsoft.public.excel.programming
stainless[_2_]
external usenet poster
 
Posts: 5
Default Defining public/global variables in more than one place

PLEASE IGNORE

Did what I should have done before posting and tried it out.

All works fine when defined as Public at each level
  #3  
Old July 13th 12, 10:44 AM posted to microsoft.public.excel.programming
Peter T[_5_]
external usenet poster
 
Posts: 83
Default Defining public/global variables in more than one place

I don't follow what you mean by "lower level sub routines", however are
variables can maintain scope (even while code is not running) at three
levels:

Global, visible throughout the project, declare at the top of a normal
module:
Public gsMyString As String

Module, visible throughout the module only, declare at the top of the
module:
Private msMyString as String

Procedure, visible only within a single routine, declare within the routine:
Static sMyString As String

Use of the End statement or anything while debugging that breaks and
prevents completion of code will reset all variables, something to keep in
mind

It's a matter of style but avoid use of such variables as "a convenience".
Rule of thumb, if the variable is not required to be stored between
subsequently running the code, pass variable as required between routines.

Regards,
Peter T


"stainless" > wrote in message
...
Using Excel 2003 (hopefully will persuade my company to allow us to upgrade
our excel reports to 2010 at some point!)

I am trying to standardise the code in our executables to make
coding/maintenance easier and have come up with the following idea:

1. Our Excel reports have a simple Auto_Open module that will be the first
piece of code in every report we have and this has 1 public variable plus
one actual command to call another module sub called Main_Processing. This
other module will contain the report relevant logic.

2. This public variable defined in the Auto_Open module is a string defining
the environment the code runs in such as Test, Development and thus can be
altered simply for each environment. Lower level code uses this to pick up
data files from the correct place for that environment.

3. This is the part my question relates to. Can the Main_Processing module
also have public variables that are used in lower level sub routines and
functions? So far, I have only defined global variables in one place at the
top level, so the question is really about scope of lower level variables.

Cheers

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Global/Public/Universal variables Matthew Dyer Excel Programming 8 September 17th 11 08:29 PM
Global/Public Fox via OfficeKB.com Excel Programming 2 May 26th 06 08:44 PM
Global variables - where do you place them? CRayF Excel Programming 16 September 28th 05 01:14 AM
problems defining a global variable filo666 Excel Programming 2 August 17th 05 10:26 PM
Public vs Global quartz[_2_] Excel Programming 2 February 9th 05 06:20 PM


All times are GMT +1. The time now is 09:33 AM.


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