Implicit Containing Workbook Reference

Locates unqualified Workbook.Worksheets/Sheets/Names member calls inside workbook document modules, that implicitly refer to the host workbook.

Reasoning

Implicit references inside a workbook document module can easily be mistaken for implicit references to the active workbook (ActiveWorkbook), which is the behavior in all other module types. By explicitly qualifying these member calls with 'Me', the ambiguity can be resolved. If the intent is to actually refer to the active workbook, qualify with 'ActiveWorkbook' to prevent a bug.

Default severity

Warning

Inspection type

CodeQualityIssues

Examples

This example should trigger a result

ThisWorkbook (DocumentModule)
Private Sub Example() Dim summarySheet As Worksheet Set summarySheet = Worksheets("Summary") ' unqualified Worksheets is implicitly querying the containing workbook's Worksheets collection. End Sub

This example should NOT trigger a result

ThisWorkbook (DocumentModule)
Private Sub Example() Dim summarySheet As Worksheet Set summarySheet = Me.Worksheets("Summary") End Sub

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