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
 
One last question,

How can I code to "copy the original sheet (before running the macro) and rename the worksheet (after running the macro) into "Macro-ed"?
Does this modified version of my code do what you want...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant, CurrentSheet As Worksheet
  Set CurrentSheet = ActiveSheet
  CurrentSheet.Copy After:=CurrentSheet
  ActiveSheet.Name = "Macro-ed"
  CurrentSheet.Activate
  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

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
HI Rick:

Happy Friday. the macro you provided worked very well. Is there any limitation how many words or phrase we can put into Array( "Bat", "Apple").

I tried to put like 130 words and I no longer to put more in the (). If there is a limitation, how can I expend to more than 200 or 300 words?

HM

Does this modified version of my code do what you want...
Code:
Sub DeleteWholeRowsForCertainWords()
  Dim V As Variant, CurrentSheet As Worksheet
  Set CurrentSheet = ActiveSheet
  CurrentSheet.Copy After:=CurrentSheet
  ActiveSheet.Name = "Macro-ed"
  CurrentSheet.Activate
  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,224,844
Messages
6,181,294
Members
453,030
Latest member
PG626

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