Delete rows which contain certain text in a cell

AndyM90

New Member
Joined
Jul 25, 2014
Messages
47
Hi All,

I have some data which I need to sort through and as part of my macro, I want to delete rows which contain certain text. So for example, in column G if the word "Late" appears in cell G50, I want to delete row 50. From other threads I have found the below code which works, but I now want to expand this to include another column, do you know how I can do this?

End point:

Column G will have no cells in it which contain the word "Late"
Column M will have no cells in it which contain the word "Cash"

Thanks!

Andy

Dim c As Range
Dim SrchRng

Set SrchRng = ActiveSheet.Range("G2:G1000")
Do
Set c = SrchRng.Find("Late", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
One way is to simply add another loop that does the exact same process on column M:
Code:
[COLOR=#333333]Set SrchRng = ActiveSheet.Range("G2:G1000")[/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]    Set c = SrchRng.Find("Late", LookIn:=xlValues)[/COLOR]
[COLOR=#333333]    If Not c Is Nothing Then c.EntireRow.Delete[/COLOR]
[COLOR=#333333]Loop While Not c Is Nothing

[/COLOR][COLOR=#333333]Set SrchRng = ActiveSheet.Range("M2:M1000")[/COLOR]
[COLOR=#333333]Do[/COLOR]
[COLOR=#333333]    Set c = SrchRng.Find("Cash", LookIn:=xlValues)[/COLOR]
[COLOR=#333333]    If Not c Is Nothing Then c.EntireRow.Delete[/COLOR]
[COLOR=#333333]Loop While Not c Is Nothing[/COLOR]
 
Upvote 0
Thanks Joe. My macro seems to be skipping this section of code now, do you know why this may be? The last action that works is sorting the data a to z, then I want to delete those rows, then continue to another section (which works).

Workbooks.Open Filename:="C:XXXXXXX"
Workbooks.Open Filename:="C:XXXXXXXX"

Application.CutCopyMode = False
Application.ScreenUpdating = False
Windows("ABC.xls").Activate
Columns("L:U").Delete
Range("A1:U500").Select
Selection.Copy
Windows("ABC.xlsx").Activate
Sheets("ABC").Range("A1").PasteSpecial xlPasteValues
Cells.EntireColumn.AutoFit
Range("A2:U380").Select
Selection.sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom



Dim c As Range
Set SrchRng = ActiveSheet.Range("G2:G1000")
Do
Set c = SrchRng.Find("L", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing


Set SrchRng = ActiveSheet.Range("D2:D1000")
Do
Set c = SrchRng.Find("Cash", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
 
Upvote 0
Have you tried to filter for the values you want to delete, then use .SpecialCells(xlCellTypeVisible) to delete the visible rows? It's usually quicker to do it this way than by looping.
 
Upvote 0
Sorry Jack, I'm a bit of a novice, can you explain .SpecialCells part in a little more detail?

My problem is that we have data which needs to be reconciled daily, and i'm trying to remove as much manual points as possible to reduce errors.
 
Upvote 0
I cannot see any reason why that would not run.
Make sure that you are on the correct file & sheet when that file runs.
It looks like you are on file ABC, on sheet ABC when it runs.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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