Inspection Details

ExcelMemberMayReturnNothing


Summary
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.
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("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

The following code example(s) would not trigger this inspection:

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


Back to List