Delete Rows When Word Is Found

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
Hi I need a macro that will look in the column I select for a specific word that could be amongst other text then delete the entire row. Also I need it to delete any blank rows that are directly beneath until it finds the next cell with data in.

i.e it looks for the word 'Hello' (maybe amongst other text) in the active column which the first may be in C12 the next 6 rows in column C may be empty so those rows need to be deleted also until the next data is found and so on..
 
I think repeat. This code will loop until you click cancel

Code:
Sub Daz()
Dim s As String, iCol As Long, LR As Long, i As Long
Dim r As Range, F As Boolean
iCol = ActiveCell.Column
s = "£"
Do While s <> ""
    s = LCase(InputBox("Enter word to look for"))
    If s = "" Then Exit Sub
    LR = Cells(Rows.Count, iCol).End(xlUp).Row
    For i = 1 To LR
        If LCase(Cells(i, iCol).Value) Like "*" & s & "*" Then
            If r Is Nothing Then
                Set r = Cells(i, iCol)
            Else
                Set r = Union(r, Cells(i, iCol))
            End If
            F = True
        ElseIf Cells(i, iCol).Value <> "" Then
            F = False
        Else
            If F Then
                If r Is Nothing Then
                    Set r = Cells(i, iCol)
                Else
                    Set r = Union(r, Cells(i, iCol))
                End If
            End If
        End If
    Next i
    If Not r Is Nothing Then r.EntireRow.Delete
    Set r = Nothing
    F = False
Loop
End Sub

Hi VoG, this works great where I highlight a column and when I enter a word in the input box and it appears anywhere in the cell it deletes the entire row. What I need now is it to do the opposite please. If I enter a word in the box it deletes all the rows that do not contain that word, which could be anywhere in the cell (and any case size). Is this possible please? Thanks.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Untested but I think this should work

Rich (BB code):
Sub Daz2()
Dim s As String, iCol As Long, LR As Long, i As Long
Dim r As Range, F As Boolean
iCol = ActiveCell.Column
s = "£"
Do While s <> ""
    s = LCase(InputBox("Enter word to look for"))
    If s = "" Then Exit Sub
    LR = Cells(Rows.Count, iCol).End(xlUp).Row
    For i = 1 To LR
        If Not LCase(Cells(i, iCol).Value) Like "*" & s & "*" Then
            If r Is Nothing Then
                Set r = Cells(i, iCol)
            Else
                Set r = Union(r, Cells(i, iCol))
            End If
            F = True
        ElseIf Cells(i, iCol).Value <> "" Then
            F = False
        Else
            If F Then
                If r Is Nothing Then
                    Set r = Cells(i, iCol)
                Else
                    Set r = Union(r, Cells(i, iCol))
                End If
            End If
        End If
    Next i
    If Not r Is Nothing Then r.EntireRow.Delete
    Set r = Nothing
    F = False
Loop
End Sub
 
Upvote 0
A slight after thought. Could I put more than one word in the box at the same time, for example if I put 'Yes,No' it will delete all the other rows that don't contain either of these? If its virtually a whole rewrite of the code then don't worry about it, only if its not too much trouble. Thanks.
 
Upvote 0
I think that you asked the same thing in Post #8. Not impossible but it would require a lot of re-writing and testing. If you really need this, please start a new thread.
 
Upvote 0
Ok thanks, I can get by using a combo of the 2 codes. I will start a new thread if I find I am using it a lot.
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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