VBA Delete rows if cell contains specific text

kevin440red

New Member
Joined
May 23, 2011
Messages
26
Office Version
  1. 2019
I found this bit of code to remove rows if it contains specific text. It runs slow on 8000 lines, would like to add more than 1 text "Dog,Cat,Chickens".
would like it to be lighting fast
Thanks

VBA Code:
Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "[COLOR=rgb(235, 107, 86)]Dog,Cat,Chickens" IN COLUMN B
'========================================================================
    Last = Cells(Rows.Count, "B").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "B").Value) = "Dog" Then
    'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End Sub
 
Then I see no reason why it won't work. Just checking though, the word being searched for is the only text in the cell (like what your originally posted code looked for), correct?
yes thats correct
error 1004 the cell changes to #N/A Delete method Range class failed
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Then I see no reason why it won't work. Just checking though, the word being searched for is the only text in the cell (like what your originally posted code looked for), correct?
Run-Time Error method of range class failed
the cells change to #N/A
 
Upvote 0
Any chance you can post your workbook on DropBox (it's free), share it and post the link they give you? That way I will be able to see what is going wrong in real time. If you have any sensitive data, make sure to replace it with nonsense text.
 
Upvote 0
Okay, I got your file, thanks for posting it... and I see the error you are talking about. Now I have to figure out why.
 
Upvote 0
Not yet (time available for volunteering has been somewhat limited), but I think it has something to do with your table.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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