English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

. What do you understand by the term “variable scope”? Give examples (code snippets) of the various variable scopes in action.

2007-01-16 23:41:33 · 3 answers · asked by Anonymous in Computers & Internet Software

3 answers

Excel VBA Variables Levels

There are 3 levels at which we can dimension (Dim) variables. These are;

1) Procedure-Level
2) Module-Level
3) Project-Level, Workbook Level, or Public Module-Level

Each of these levels differ in scope and lifetime. This is discussed below

Procedure-Level Variables

These are probably the best known and widely used variables. They are dimensioned (Dim) inside the Procedure itself. See Example below;

Sub MyMacro ()
Dim lRows as Long
'Code Here
End Sub

All variables dimensioned at this level are only available to the Procedure that they are within. As soon as the Procedure finishes, the variable is destroyed.

Module-Level Variables

These are variables that are dimensioned (Dim) outside the Procedure itself at the very top of any Private or Public Module. See Example below;

Dim lRows as Long

Sub MyMacro ()
'Code Here
End Sub

All variables dimensioned at this level are available to all Procedures that they are within the Module it is dimensioned in. It's value is retained unless the variable is referenced outside its scope, the Workbook closes or the End Statement is used.

Project-Level, Workbook Level, or Public Module-Level

These variables are dimensioned at the top of any standard public module, like shown below;

Public lRows as Long

All variables dimensioned at this level are available to all Procedures in all Modules. It's value is retained unless the Workbook closes or the End Statement is used.

Look at the site where I just copied the information from, this should answer your question.

2007-01-17 00:29:01 · answer #1 · answered by AMTV 3 · 0 0

Dude, do you want to get banned? It's against policy to repeatedly ask the same question to increase your chance of getting an answer. Abuse reported.

2007-01-16 23:51:33 · answer #2 · answered by yoink78 2 · 0 0

you my friend are annoying stop posting 20 damn questions post one then wait for an answer then post another give other people a chance to ask questions to dillweed

2007-01-16 23:45:51 · answer #3 · answered by justin_jordan1981 2 · 0 0

fedest.com, questions and answers