Inspection Details
ImplicitActiveSheetReference
- Summary
- Locates unqualified Worksheet.Range/Cells/Columns/Rows member calls that implicitly refer to ActiveSheet.
- Reasoning
- Implicit references to the active worksheet rarely mean to be working with *whatever worksheet is currently active*.
By explicitly qualifying these member calls with a specific Worksheet object, the assumptions are removed, the code
is more robust, and will be less likely to throw run-time error 1004 or produce unexpected results
when the active sheet isn't the expected one.
- This inspection will only run if the following libraries referenced:
-
Excel
- The following code example(s) would trigger this inspection:
-
Private Sub Example()
Dim foo As Range
Set foo = Sheet1.Range(Cells(1, 1), Cells(1, 10))
End Sub
- The following code example(s) would not trigger this inspection:
-
Private Sub Example()
Dim foo As Range
With Sheet1
Set foo = .Range(.Cells(1, 1), .Cells(1, 10))
End With
End Sub
Back to List