Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 920
Also called as Local Variables, all Procedure-Level variables are accessible only within the procedure or Function in which they are declared. As soon as the procedure finishes, the variable lost its scope.
In the following example, iCntr is a Local Variable which can be only accessible in this procedure.
Sub sbScopeProcedureLevel() Dim iCntr As Integer iCntr = 2000 MsgBox "Example of a Procedure level Variable: " & iCntr End Sub
All Procedure-Level variables are accessible only within the Module in which they are declared. These are variables that are declared outside the Procedure itself at the very top of any Module. Its value is retained unless the Workbook closes or an End Statement is used.
In the following example, lRow can be accessible any procedure in the Module in which it is declared.
Option Explicit Dim lRow As Long Sub sbProcedure1() MsgBox "Example of a Module Level Variable " & lRow End Sub
Sub sbProcedure2() MsgBox "Example of a Module Level Variable " & lRow End Sub
All Global-Level variables are accessible in anywhere in the Project (.i.e; in any Module, User Form, Classes) within the Workbook in which they are declared. And also accessible to outside of this project or workbook. These are variables that are declared using ‘Public’ keyword at the very top of any Public Module .
In the following example, lRow can be accessible any procedure in the project or workbook and also out-side of the module.
Option Explicit
Public lRow As Long Sub sbProcedure1() lRow = 220 MsgBox "Example of a Public Level Variable " & lRow End Sub
Sub sbProcedure2() MsgBox "Example of a Public Level Variable " & lRow End Sub
We set Project -Level Scope to the variables if we want to make the public variable to be accessed only in the project in which they are declared and not out side of this project. To set this option we need to add “Option Private Module” statement at the top of the declaration area.
In the following example, lRow can be accessible any procedure in the project or workbook only in which it is declared.
Option Explicit Option Private Module Public lRow As Long Sub sbProcedure1() lRow = 220 MsgBox “Example of a Public Level Variable ” & lRow End Sub
Sub sbProcedure2() MsgBox “Example of a Public Level Variable ” & lRow End Sub