Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 143
Default Global vs Local variables

In the project I am working on there are a number of variables that I have
made local, but now wonder if they might not be better suited as global.

For example, the sortByName macro does just what its name implies and sorts
the worksheet by name based upon the name being in column 2. I use a local
variable to store that, i.e., nameCol = 2. But there are other macros that
need to know that the name column is column 2. In this project I always had
nameCol as a local variable for each macro that needed it.

But now the report format is changing and I need to change some of those
column numbers. Not such big deal, since I just search for "nameCol = 2" and
replace with "nameCol = 3" and that fixes that.

But I just thought it might be a little easier to change and easier to read,
if I used global variables for these types of values.

Can anybody tell me if the advantages or disadvantages are significant for
local vs. global variables in VBA? And, if so, what are those?





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Global vs Local variables

In this situation, I set them up as public constants in one of the modules.
One cited disadvantage of a project-level variable is that any sub in any
module can change its value. But if you make these constants, that problem is
eliminated.

On Mon, 11 Oct 2004 18:22:24 -0700, "Ken Loomis" wrote:

In the project I am working on there are a number of variables that I have
made local, but now wonder if they might not be better suited as global.

For example, the sortByName macro does just what its name implies and sorts
the worksheet by name based upon the name being in column 2. I use a local
variable to store that, i.e., nameCol = 2. But there are other macros that
need to know that the name column is column 2. In this project I always had
nameCol as a local variable for each macro that needed it.

But now the report format is changing and I need to change some of those
column numbers. Not such big deal, since I just search for "nameCol = 2" and
replace with "nameCol = 3" and that fixes that.

But I just thought it might be a little easier to change and easier to read,
if I used global variables for these types of values.

Can anybody tell me if the advantages or disadvantages are significant for
local vs. global variables in VBA? And, if so, what are those?





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
How can I change a Name scope from local to Global? Henry Excel Discussion (Misc queries) 7 April 9th 10 10:36 PM
How can I change a Name scope from local to Global? Henry Excel Discussion (Misc queries) 0 September 23rd 08 07:24 PM
Global name used in local version clara Excel Discussion (Misc queries) 3 June 16th 08 06:54 PM
User Defined Functions - local or global? Simon Excel Worksheet Functions 5 September 27th 06 09:15 AM
Referring to a local or global variable or constant dynamically? PC[_2_] Excel Programming 1 September 15th 03 02:31 PM


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

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"