Home > Blockchain >  Find Not Returning Value Even When Present
Find Not Returning Value Even When Present

Time:01-10

I'm trying to find a value in a column, then add a row after the last row with data after the found value. The issue I'm encountering is when finding the value using the Find function - when testing I receive

Runtime Error 91: Object Variable of With block variable not set

During my troubleshooting I found that this error gets thrown when trying to access an attribute of the result of a Find that did not find a match. However, I know there is a match within the given range.

Private Sub CommandButton1_Click()
    Dim FirstRow As Range
    
    'issue here
    FirstRow = Range("B:B").Find("SearchTerm")
    
    ' Find last row with data
    With Range(FirstRow.Address).CurrentRegion
        LastRow = .Rows(.Rows.Count).Row
    End With
    
    'add row
    Rows(LastRow & ":" & LastRow).Insert Shift:=x1Down
End Sub

Help would be very much appreciated!

CodePudding user response:

You don't have your syntax correct for setting an object variable. You need to use Set.

FirstRow = Range("B:B").Find("SearchTerm")

should be:

Set FirstRow = Range("B:B").Find("SearchTerm")
  •  Tags:  
  • Related