Macro to delete row if doesn't contain certain text

srr797

New Member
Joined
Nov 6, 2010
Messages
39
Hello,

I am in need of what seems to be a fairly simple macro, which will delete (and shift up) any row that does not contain the text "IP-0000063409" in column E.


Thanks very much!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Can't you try filtering the data in column E to only show your required text, and then copying remaining data to a clean sheet? That way it retains existing info for clarity.
 
Upvote 0
Don't need the existing info... would like a macro so it would be automated every time the list is regenerated.
 
Upvote 0
For that size, try this:

Code:
Sub Macro2()
        
    '//Declare variables//
    Dim varDelItem As Variant
    Dim lngRowStart As Long, _
        lngRowLast As Long, _
        lngRowActive As Long
    Dim strMyCol As String
    Dim rngDelRange As Range
    
    '//Set variables//
    varDelItem = "IP-0000063409"
    lngRowStart = 2 'Initial data row.  Change to suit.
    strMyCol = "E" 'Column containing relevant data.  Change to suit.
    lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row
    
    Application.ScreenUpdating = False
        
    For lngRowActive = lngRowStart To lngRowLast
        If Cells(lngRowActive, strMyCol) <> varDelItem Then
            'Cater for initial setting of 'rngDelRange' range
            If rngDelRange Is Nothing Then
                Set rngDelRange = Cells(lngRowActive, strMyCol)
            Else
                Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
            End If
        End If
    Next lngRowActive
        
    'If the 'rngDelRange' range has been set (i.e. has something in it), then...
    If Not rngDelRange Is Nothing Then
        '...delete the rows within it.
        rngDelRange.EntireRow.Delete xlShiftUp
    'Else...
    Else
        '...inform the user that no rows are to be deleted as there was no _
        matching criteria in the dataset.
        MsgBox "No rows were deleted as every Row in Column " & strMyCol & " matched """ & varDelItem & """.", vbExclamation, "Delete Row Editor"
    End If
    
    Application.ScreenUpdating = True
        
End Sub

Robert
 
Upvote 0
Thanks Robert. There seems to be a problem... when I run the macro it deletes all my data but the first row.
 
Upvote 0
Hi,

try this

code:
Sub DELROWs()
Dim couNter As Long
Dim RowCount As Long
Application.ScreenUpdating = False
RowCount = Range("E65536").End(xlUp).Row
couNter = 1
Do Until couNter > RowCount
If Range("E" & couNter).Value = "IP-0000063409" Then
Range("E" & couNter).EntireRow.Delete
RowCount = RowCount - 1
couNter = couNter - 1
End If
couNter = couNter + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Robert. There seems to be a problem... when I run the macro it deletes all my data but the first row.

That's odd - it worked fine for me :confused:

Are there any trailing spaces either before and/or after each entry in Col E? If you also want to include Row 1, simply change the 'lngRowStart' variable.
 
Last edited:
Upvote 0
Hello,

I am in need of what seems to be a fairly simple macro, which will delete (and shift up) any row that does not contain the text "IP-0000063409" in column E.


Thanks very much!

Here's another approach:

Code:
Sub srr797()
Dim lr As Long
Dim i As Long

lr = Cells(Rows.Count, 5).End(xlUp).Row

    For i = lr To 2 Step -1
    
        If Range("E" & i) <> "IP-0000063409" Then
        
            Range("E" & i).EntireRow.Delete
            
        End If
    
    Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,703
Members
452,938
Latest member
babeneker

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