VBA Delete Entire Row if Contains Certain Text

bigmacneb

Board Regular
Joined
Jul 12, 2005
Messages
93
I've searched on here, but every code I put in gives me an error back. Data in column D, If any of the cells contains "Record Only" I need it to delete the entire row.
Thanks
 
SiuGuy007,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


If I understand you correctly, then here is another macro solution for you to consider, that does not do any looping thru the rows, and, does not use AutoFilter, and, should be very fast.

Sample raw data in the active worksheet:


Excel 2007
AW
1Title ATitle W
2I am Well
3I am not Well
4Alive Well
5I am Well
6I am not Well
7Alive Well
8I am Well
9Alive Well
10I am Well
11Alive Well
12I am not Well
13
Sheet1


And, after the macro:


Excel 2007
AW
1Title ATitle W
2I am Well
3I am not Well
4I am Well
5I am not Well
6I am Well
7I am Well
8I am not Well
9
10
11
12
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub SiuGuy007()
' hiker95, 03/04/2016, ME300330
Columns("W").Replace "Alive Well", "#N/A", xlWhole
On Error Resume Next
Columns("W").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the SiuGuy007 macro.
 
Last edited:
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Thanks again...I am a VBA newb, so please forgive me. I copied the Code and run it. Again, not changes to the spreadsheet. In column W, the text will be Alive/Well Check or Alive/Well Check, Activity Check.
When the code looks for the word does it have to be an exact match or if it find the 2 words, even if there are additional words, coma, slash etc?


SiuGuy007,

It looks like I did not understand you correctly.

Can you supply a list of separate strings that can be in the cells in column W (beginning in W2), and, which one/ones you want to delete the rows for?
 
Last edited:
Upvote 0
Upvote 0
SiuGuy007,

Thanks for the workbook/worksheet.

Here is a new macro for you to consider based on the new raw data structure.

Sample raw data:


Excel 2007
ABCSTUVWX
1Case IDMaster Case IDCase StatusServiceDaysVendorVendor TypeOfficeIDOffice
2396640257222Case CompletedAlive/Well Check0.0NBAGO37FW
3396721257263Case CompletedAlive/Well Check0.0NBAGO38SA
4396721257263Case CompletedCounty Criminal History0.0NBAGO38SA
5396721257263Case CompletedCounty Civil Records0.0NBAGO38SA
6
ServRaw


And, after the new macro:


Excel 2007
ABCSTUVWX
1Case IDMaster Case IDCase StatusServiceDaysVendorVendor TypeOfficeIDOffice
2396721257263Case CompletedCounty Criminal History0.0NBAGO38SA
3396721257263Case CompletedCounty Civil Records0.0NBAGO38SA
4
5
6
ServRaw


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub SiuGuy007_V2()
' hiker95, 03/04/2016, ME300330
Columns("S").Replace "Alive/Well*", "#N/A", xlWhole
On Error Resume Next
Columns("S").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the SiuGuy007_V2 macro.
 
Upvote 0
SiuGuy007,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
If I have other rows that I wanted to delete, I would just change the name in Column Replace line, correct? What is the name is similar to this "ESES (245)"?
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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