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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Give this macro a try...
VBA Code:
Sub DeleteRowWithContents()
  Dim V As Variant
  For Each V In Array("Dog", "Cat", "Chickens")
    Columns("B").Replace V, "#N/A", xlWhole, , True, , False, False
  Next
  Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
End Sub
 
Upvote 0
Give this macro a try...
VBA Code:
Sub DeleteRowWithContents()
  Dim V As Variant
  For Each V In Array("Dog", "Cat", "Chickens")
    Columns("B").Replace V, "#N/A", xlWhole, , True, , False, False
  Next
  Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
End Sub
getting error on line 6

Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
 
Upvote 0
What is the error number and error description? By the way, I test the code and it worked on my test.
 
Upvote 0
Yes, if Column B has formulas in it, the method I posted won't work.
 
Upvote 0
Does Column B have any of the keywords being searched for (Cat, Dog or Chickens)?
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,527
Members
449,037
Latest member
tmmotairi

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