Procedure-level variables
If you declare a variable within a procedure or function, it will be known within that procedure or function only. For example:
Code: Select all
Sub Test1()
Dim MyVar As String
MyVar = "Lounge"
End Sub
Normally, the value of a local variable is forgotten once the procedure or function ends. But if you declare it using the keyword Static instead of Dim, the variable will retain its value when the procedure or function ends, until you close the document/workbook/… or an unhandled error occurs.
Example:
Code: Select all
Sub Test2()
Static MyVar As Long
MyVar = MyVar + 1
End Sub
Module-level variables
If you declare a variable at the top of a module with the keyword Dim or Private, it will be known within all procedures and functions in that module, but not in other modules within the same document, workbook etc. For example:
Code: Select all
Dim MyVar As Long
Sub Test1()
MyVar = MyVar + 2
End Sub
Sub Test2()
MyVar = MyVar - 3
End Sub
Code: Select all
Private MyVar As Long
Global variables
If you declare a variable using the keyword Public, it will be known to all procedures and functions in all modules in the same document, workbook etc. For example:
Public MyVar As Long
MyVar is called a public or global variable.
Declaring a variable at more than one level
It is possible to declare a variable with the same name as a local variable and as a private or public variable, or as a private variable and as a public variable, or even as all three. This can easily lead to confusion, so I would avoid it if possible. But if you do so, the code will see the lowest-level declaration. For example:
Code: Select all
Private MyVar As String
Sub Test1()
Dim MyVar As String
MyVar = "Eileen"
Debug.Print "In Test1: " & MyVar
End Sub
Sub Test2()
MyVar = "Lounge"
Debug.Print "In Test2, before: " & MyVar
Call Test1
Debug.Print "In Test2, after: " & MyVar
End Sub
Code: Select all
In Test2, before: Lounge
In Test1: Eileen
In Test2, after: Lounge
Constants
Constants are declared with the keyword Const, for example:
Code: Select all
Const MyDrive = "C:"
A constant declared at the top of a module is private by default:
Code: Select all
Const MyDrive = "C:"
Code: Select all
Private Const MyDrive = "C:"
Code: Select all
Public Const MyDrive = "C:"
By default, procedures and functions are public, i.e. they can be called in all modules in the same document, workbook etc. The procedure header
Code: Select all
Sub Test1()
Code: Select all
Public Sub Test1()
Code: Select all
Private Sub Test2()
...
End Sub
It is possible to create a private procedure or function with the same name in more than one module.
It is also possible to create a public procedure or function in one module, and a private procedure or function with the same name in another module. If you call the name from any module except the one with the private version, the public version will be run. In the module with the private version, that will be run.
As with variables, although this is valid, it can easily lead to confusion, so in general I'd avoid it.
A final warning
Please note that the default behavior for procedures/functions is the opposite of the default behavior of variables and constants at the top of a module: a procedure/function is public unless you explicitly use the keyword Private in its definition, but a variable/constant is private unless you explicitly use the keyword Public in its declaration. For example:
Code: Select all
Dim MyVar As String
Sub Test1()
...
End Sub