Delete the whole rows that contains certain words and exact words in the column

happymacro

New Member
Joined
Nov 4, 2017
Messages
18
Hi there:

I tried to delete the whole row if the column contains certain words for example if a column contain "Labor Cost", I want to delete the entire row. The macro I have only can delete exact words that I put in but I would really like to delete both exact words and contain certain words.

Here is what I have but not sure where went wrong.

Dim lastRow As Long
Dim thisRow As Long


Sheets("Sitedata").Select


lastRow = Cells(Rows.Count, 1).End(xlUp).Row


For thisRow = lastRow To 1 Step -1


Select Case Cells(thisRow, 1).Value
Case "Bat", "Apple", Order ID", "Orange"
Cells(thisRow, 1).EntireRow.Delete

End Select

Next thisRow


Sheets("Sitedata").Select


lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For thisRow = lastRow To 1 Step -1




If Cells(thisRow, 1).Value = "LABOR Cost" Then
Cells(thisRow, 1).EntireRow.Delete




End If




Next thisRow




End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Give this macro a try...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant
  For Each V In Array("Bat", "Apple", "Order ID", "Orange")
    Columns("A").Replace V, "#N/A", xlPart, , False, , False, False
  Next
  On Error GoTo NoSuchWords
  Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoSuchWords:
End Sub
Note: I am a little concerned about false positive matches. For example, you are looking for the word "bat", but the code I posted would also flag words like "battle", "combat", etc.
 
Last edited:
Upvote 0
No loop needed for this coding?

Give this macro a try...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant
  For Each V In Array("Bat", "Apple", "Order ID", "Orange")
    Columns("A").Replace V, "#N/A", xlPart, , False, , False, False
  Next
  On Error GoTo NoSuchWords
  Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoSuchWords:
End Sub
Note: I am a little concerned about false positive matches. For example, you are looking for the word "bat", but the code I posted would also flag words like "battle", "combat", etc.
 
Upvote 0
Hi Rich:

It worked partially and I want to remove the entire row whenever the column contain these words.

For example if a phase { ABC "Labor Cost"}, this phase contains "labor cost", I want the whole {ABC Labor Cost} row column deleted.

The codes you wrote here replace labor cost to #N/A. how can I revise this?

There is a For Each loop in my code, but only of the words you are searching for... Excel's Replace functionality can handle multiple cells at at once as can SpecialCells.
 
Upvote 0
Hi Rich:

It worked partially and I want to remove the entire row whenever the column contain these words.

For example if a phase { ABC "Labor Cost"}, this phase contains "labor cost", I want the whole {ABC Labor Cost} row column deleted.

The codes you wrote here replace labor cost to #N/A. how can I revise this?
Does this revised code do what you want...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant
  For Each V In Array("Bat", "Apple", "Order ID", "Orange")
    Columns("A").Replace "*" & V & "*", "#N/A", xlWhole, , False, , False, False
  Next
  On Error GoTo NoSuchWords
  Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoSuchWords:
End Sub
 
Upvote 0
WOW!!!!!! Perfect! Thank you so much, Rick.

Does this revised code do what you want...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant
  For Each V In Array("Bat", "Apple", "Order ID", "Orange")
    Columns("A").Replace "*" & V & "*", "#N/A", xlWhole, , False, , False, False
  Next
  On Error GoTo NoSuchWords
  Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoSuchWords:
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,920
Messages
6,175,376
Members
452,638
Latest member
Oluwabukunmi

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