Inspection Details

MoveFieldCloserToUsage


Summary
Locates module-level fields that can be moved to a smaller scope.
Reasoning
Module-level variables that are only used in a single procedure can often be declared in that procedure's scope. Declaring variables closer to where they are used generally makes the code easier to follow.
The following code example(s) would trigger this inspection:

Option Explicit
Private foo As Long
Public Sub DoSomething()
    foo = 42
    Debug.Print foo ' module variable is only used in this scope
End Sub

The following code example(s) would not trigger this inspection:

Option Explicit
Public Sub DoSomething()
    Dim foo As Long ' local variable only used in this scope
    foo = 42
    Debug.Print foo
End Sub


Back to List