VBA Delete Entire Row if Contains Certain Text

bigmacneb

Board Regular
Joined
Jul 12, 2005
Messages
93
I've searched on here, but every code I put in gives me an error back. Data in column D, If any of the cells contains "Record Only" I need it to delete the entire row.
Thanks
 
Just a little "bump"

Very helpfull thread. Is there a way to "clear" entire row if column "A" CONTAINS ANY TEXT?
Or if column "A" AND "B" contains any text?
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I am running into a similiar problem....I need to delete a row if for example column "B" has specific text and column "C" has specific text then delete if one is not true then don't delete
 
Upvote 0
I used the VBA code supplied above from the 2nd person who responded. This works great; however, I have over 50,000 rows of data and it is taking an extremely long time to process through this macro. Is there any way to speed up the overall process of this code?

Code:
Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "Record Only" IN COLUMN D
'========================================================================
    Last = Cells(Rows.Count, "G").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "G").Value) = "Balanced" 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
 
Upvote 0
Hi I have used both sets of code within the thread. This successfully deletes or autofilters and deletes one set of criteria. I have the following items that I need to deleted the whole row for.
PAI, MPD, Private Contractor, Local Council. I can succesfully delete one item at a time.

Is there a way to specify in either code multiple criteria I have tried ands and ors with no success. I am new to this guys so any help would be appreciated. The column I need to autofilter or search is "R" and delete all rows that contain any of the four item MPD, PAI, Private Contractor or Local Council
cheers and thanks
Ron
 
Upvote 0
Hi I have used both sets of code within the thread. This successfully deletes or autofilters and deletes one set of criteria. I have the following items that I need to deleted the whole row for.
PAI, MPD, Private Contractor, Local Council. I can succesfully delete one item at a time.

Is there a way to specify in either code multiple criteria I have tried ands and ors with no success. I am new to this guys so any help would be appreciated. The column I need to autofilter or search is "R" and delete all rows that contain any of the four item MPD, PAI, Private Contractor or Local Council
cheers and thanks
Ron
Hey Ron,
Try this one:
Code:
Sub test()
Dim x, lr As Long, lc As Integer
Dim a, b() As Variant, i As Long, e
x = Array("MPD", "PAI", "Private Contractor", "Local Council")
lr = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc = ActiveSheet.Cells.Find("*", searchorder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
If lc < 18 Then
    MsgBox "Column R is unused" & Chr(10) & "Exiting"
    Exit Sub
End If
a = Cells(1, "r").Resize(lr)
ReDim b(1 To lr, 1 To 1)
For i = 1 To lr: For Each e In x
    If InStr(a(i, 1), e) Then b(i, 1) = 1
Next e, i
Cells(1, lc + 1).Resize(lr) = b
Cells(1, 1).Resize(lr, lc + 1).Sort Cells(1, lc + 1), 1
Cells(1, 1).Resize(Cells(1, lc + 1).End(xlDown).Row, lc + 1).Delete 3
End Sub
 
Upvote 0
I used the VBA code supplied above from the 2nd person who responded. This works great; however, I have over 50,000 rows of data and it is taking an extremely long time to process through this macro. Is there any way to speed up the overall process of this code?
aeddipa,

with 50k rows the row by row deletion approach (EntireRow.delete etc) can be pretty slow.

If you're still active on this topic, the code in my preceding post can be easily modified for your case to do your problem much faster.
 
Upvote 0
Hi
Tried the code but it deleted everything in the sheet. Thanks I will play with the code and see if I can get it to work.


cheers

Ron
 
Upvote 0
Tried the code but it deleted everything in the sheet
I think this would only happen if none of your specified strings occur in Column R.

However I obviously overlooked that case when doing the code.

Try this modification and see if it does all OK
Code:
Sub test2()
Dim x, lr As Long, lc As Integer
Dim a, b() As Variant, i As Long, e, k As Boolean
Application.ScreenUpdating = False
x = Array("MPD", "PAI", "Private Contractor", "Local Council")
lr = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
lc = ActiveSheet.Cells.Find("*", searchorder:=xlByColumns, _
    searchdirection:=xlPrevious).Column
If lc < 18 Then
    MsgBox "Column R is unused" & Chr(10) & "Exiting"
    Exit Sub
End If
a = Cells(1, "r").Resize(lr)
ReDim b(1 To lr, 1 To 1)
For i = 1 To lr: For Each e In x
    If InStr(a(i, 1), e) > 0 Then
        b(i, 1) = 1
        k = True
    End If
Next e, i
If k = False Then Exit Sub
Cells(1, lc + 1).Resize(lr) = b
Cells(1, 1).Resize(lr, lc + 1).Sort Cells(1, lc + 1), 1
Cells(1, 1).Resize(Cells(1, lc + 1).End(xlDown).Row, lc + 1).Delete 3
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi mate

My apologies. I have been diverted by some family issues. Have got back to this and it worked fine, thank you my friend.

cheers

ron
 
Upvote 0
I've found that any code for this that requires looping for this task takes a long time.

Simple but effective and also 100% recordable, I just:

Autofilter what I want to see
copy all cells
create new sheet
paste
delete original data
paste filtered data back in from your new sheet
delete the sheet you created

;)
 
Upvote 0

Forum statistics

Threads
1,214,577
Messages
6,120,359
Members
448,956
Latest member
Adamsxl

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