Implicit Active Sheet Reference

Locates unqualified Worksheet.Range/Cells/Columns/Rows member calls implicitly referring to ActiveSheet.

Reasoning

Implicit references to the active worksheet (ActiveSheet) 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.

Default severity

Warning

Inspection type

LanguageOpportunities

This inspection will only run when the Excel library is referenced

Examples

This example should trigger a result

MyModule (StandardModule)
Private Sub Example() Dim foo As Range Set foo = Sheet1.Range(Cells(1, 1), Cells(1, 10)) ' Worksheet.Cells implicitly from ActiveSheet; error 1004 if that isn't Sheet1. End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Private Sub Example() Dim foo As Range With Sheet1 Set foo = .Range(.Cells(1, 1), .Cells(1, 10)) ' all member calls are made against the With block object End With End Sub

Rubberduck.CodeAnalysis.Inspections.Concrete.ImplicitActiveSheetReferenceInspection.cs (Prerelease-v2.5.9.6289)