Delete Rows That Do Not Contain a Certain Phrase

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Struggling to make a for loop that'll delete a cell in column A if it doesn't contain the word "Lot" amongst the string of text.

Example:
G:\JOBCARDS\ADS\ERT\ADSERT78-85PHS11-PC5.xls
The following above would be deleted, since it doesn't contain the word "Lot" within it

G:\JOBCARDS\ADS\ERT\Lot2032.xls
This row would remain, since it does contain the word "Lot"
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi Ottsel,

you could try this routine ? It will look for last row, then cycle backwards through the data, just clearing the contents of each cell that does not have "lot" inside it.


VBA Code:
Sub delete_row()

Dim datalastrow, x As Long
Dim MyText As String

datalastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' find the last row of data

For x = datalastrow To 1 Step -1

    MyText = ActiveSheet.Range(Cells(x, 1), Cells(x, 1))
   
    If Not (InStr(1, MyText, "LOT", vbTextCompare)) Then
   
        'delete cell
        Range(Cells(x, 1), Cells(x, 1)).ClearContents
   
    End If

Next x

End Sub

cheers
Rob
 
Upvote 0
Solution
you could highlight the column of data and on the data tab, select filter. Click on the drop down and select "Text Filters" and then select "does not contain" and fill in "Lot"
 
Upvote 0
VBA Code:
Sub delete_row()

Dim datalastrow, x As Long
Dim MyText As String

datalastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' find the last row of data

For x = datalastrow To 1 Step -1

    MyText = ActiveSheet.Range(Cells(x, 1), Cells(x, 1))
  
    If Not (InStr(1, MyText, "LOT", vbTextCompare)) Then
  
        'delete cell
        Range(Cells(x, 1), Cells(x, 1)).ClearContents
  
    End If

Next x

End Sub

cheers
Rob
Worked wonders. Appreciate it Rob!
 
Upvote 0
you could highlight the column of data and on the data tab, select filter. Click on the drop down and select "Text Filters" and then select "does not contain" and fill in "Lot"
Normally yes, but I need the rows removed for the export. I'm having this added to an already existing macro to help increase the overall process.
The system will pick up the hidden rows.
 
Upvote 0
I'm confused, did you want the row removed or just the cell value? It makes a difference.
 
Upvote 0
If the entire row should be removed ...

Perhaps something like:
VBA Code:
Sub KeepRowsWhereColumnAEqualLot()
'
    With ActiveSheet
        .AutoFilterMode = False                                             '   Remove filter
        .UsedRange.AutoFilter Field:=1, Criteria1:="<>" & "*Lot*"           '   Look for cells in Column A that <> "*Lot*"
        .AutoFilter.Range.EntireRow.Delete                                  '   Delete the rows that 'passed inspection'
        .AutoFilterMode = False                                             '   Remove filter which leaves remaining rows
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,539
Members
449,316
Latest member
sravya

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