VBA delete row if contains specific text in column

ahansche

New Member
Joined
Jun 1, 2015
Messages
4
Hi,

I am trying to run a macro to delete an entire row If the value in column D contains a certain text. I am using the autofilter method. Here is my code:

Sub test()
With ActiveSheet
.AutoFilterMode = False
With Range("d1", Range("d" & Rows.Count).End(xlUp))
.AutoFilter 1, "*AL03*"
.AutoFilter 1, "*AL23*"
.AutoFilter 1, "*AN06*"
.AutoFilter 1, "*CA08*"
.AutoFilter 1, "*CA10*"
.AutoFilter 1, "*CA12*"
.AutoFilter 1, "*CC12*"
.AutoFilter 1, "*CC14*"
.AutoFilter 1, "*FA40*"
.AutoFilter 1, "*HZ36*"
.AutoFilter 1, "*HZ37*"
.AutoFilter 1, "*HZ38*"
.AutoFilter 1, "*HZ40*"
.AutoFilter 1, "*HZ41*"
.AutoFilter 1, "*HZ47*"
.AutoFilter 1, "*HZ49*"
.AutoFilter 1, "*HZ56*"
.AutoFilter 1, "*HZ59*"
.AutoFilter 1, "*MP01*"
.AutoFilter 1, "*SI*"
.AutoFilter 1, "*ST86*"
.AutoFilter 1, "*ANO2*"
.AutoFilter 1, "*AP01*"
.AutoFilter 1, "*CA07*"
.AutoFilter 1, "*CA09*"
.AutoFilter 1, "*CC05*"
.AutoFilter 1, "*CC17*"
.AutoFilter 1, "*FS50*"
.AutoFilter 1, "*GL10*"
.AutoFilter 1, "*HP*"
.AutoFilter 1, "*HZ34*"
.AutoFilter 1, "*HZ35*"
.AutoFilter 1, "*HZ37*"
.AutoFilter 1, "*HZ39*"
.AutoFilter 1, "*HZ40*"
.AutoFilter 1, "*HZ41*"
.AutoFilter 1, "*HZ46*"
.AutoFilter 1, "*HZ48*"
.AutoFilter 1, "*HZ50*"
.AutoFilter 1, "*HZ52*"
.AutoFilter 1, "*HZ53*"
.AutoFilter 1, "*HZ61*"
.AutoFilter 1, "*SAHZ*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub

What am I doing wrong?

Thanks!

Alex
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

you need to filter and delete one criteria at a time

Code:
With Range("d1", Range("d" & Rows.Count).End(xlUp))
.AutoFilter 1, "*AL03*"
.Offset(1).SpecialCells(12).EntireRow.Delete
.AutoFilter 1, "*AL23*"
.Offset(1).SpecialCells(12).EntireRow.Delete

and so on.

don't think you need to on error code. Would you ever have more than one instance of the text?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,195,591
Messages
6,010,614
Members
441,558
Latest member
lambierules

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
Top