Handle empty search results

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a sheet that my macro searches in colA for a specific text. I want to delete rows but only in instances where the actual text string was found. I am running into an Error Type 91, and I know it is because the search string did not turn up any results, and when I assigned those "non-results" to a term "PDARow" and then use that term in an IF Then statement, it fails. I would like to assign some text to the term "PDARow" when the search results are nothing (say "notext"). That way I can write If PDARow.value = "notext" then and the macro should work fine. How do I make this assignment when the search results on nothing?

VBA Code:
Dim PDARow As Range
Dim MISCRow As Range

With Sheets("Component List")
        Set PDARow = .Range("A:A").Find(What:="PROCESS DEVELOPMENT ASSISTANCE (per hour)", LookIn:=xlValues)
    End With
    
    With Sheets("Component List")
        Set MISCRow = .Range("A:A").Find(What:="MISCELLANEOUS", LookIn:=xlValues)
    End With

    If MISCRow.Value = "" Then 'place "notext" here to fix error 91
        If PDARow.Value = "" Then 'place "notext" here to fix error 91
            Sheets("Component List").Rows(ATISRow.Row & ":" & newLrow - 1).EntireRow.Delete
        Else
            Sheets("Component List").Rows(PDARow.Row & ":" & newLrow - 1).EntireRow.Delete
        End If
    Else
        If PDARow.Value = "" Then 'place "notext" here to fix error 91
            Sheets("Component List").Rows(ATISRow.Row & ":" & newLrow - 1).EntireRow.Delete
        Else
            Sheets("Component List").Rows(PDARow.Row & ":" & MISCRow.Row - 1).EntireRow.Delete
        End If
    End If

Thanks for the help.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I got it. I co-worker who knows a little code gave me the clue. my solution is below.

VBA Code:
    If MISCRow Is Nothing Then
        If PDARow Is Nothing Then
            Sheets("Component List").Rows(ATISRow.Row & ":" & newLrow - 1).EntireRow.Delete
        Else
            Sheets("Component List").Rows(PDARow.Row & ":" & newLrow - 1).EntireRow.Delete
        End If

Thanks for taking a moment to look it over though. The support here is amazing.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top