Implicit Active Workbook Reference

Locates unqualified Workbook.Worksheets/Sheets/Names member calls that implicitly refer to ActiveWorkbook.

Reasoning

Implicit references to the active workbook rarely mean to be working with *whatever workbook is currently active*. By explicitly qualifying these member calls with a specific Workbook 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 workbook 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 summarySheet As Worksheet Set summarySheet = Worksheets("Summary") ' unqualified Worksheets is implicitly querying the active workbook's Worksheets collection. End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Private Sub Example(ByVal book As Workbook) Dim summarySheet As Worksheet Set summarySheet = book.Worksheets("Summary") End Sub

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