Excel Member May Return Nothing

Locates instances of member calls made against the result of a Range.Find/FindNext/FindPrevious method, without prior validation.

Reasoning

Range.Find methods return a Range object reference that refers to the cell containing the search string; this object reference will be Nothing if the search didn't turn up any results, and a member call against Nothing will raise run-time error 91.

Default severity

Warning

Inspection type

CodeQualityIssues

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("A:A").Find("Test") ' foo is Nothing if there are no results MsgBox foo.Address ' Range.Address member call should be flagged. Dim rowIndex As Range rowIndex = Sheet1.Range("A:A").Find("Test").Row ' Range.Row member call should be flagged. End Sub

This example should NOT trigger a result

MyModule (StandardModule)
Private Sub Example() Dim foo As Range Set foo = Sheet1.Range("A:A").Find("Test") If Not foo Is Nothing Then MsgBox foo.Address ' Range.Address member call is safe. End If End Sub

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