Delete lines with a string in cells

slick2000

New Member
Joined
Aug 6, 2021
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
I am looking for a simple VBA function that will delete a line if there is a certain string inside a cell.
The string does NOT have to be exact.
Example: I want to delete all lines that have the word "string1" inside them, the strings are always in column C
cell C2 contains : "bla bla bla string1 bla bla"
cell C87 contains :" bla string1 bla bla bla bla".
cell C1234 : "string1 bla bla bla

etc

thank you very much !

Nicola
 

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
Hi & welcome to MrExcel.
How about
VBA Code:
Sub slick()
   With ActiveSheet
      .Range("A1:C1").AutoFilter 3, "*string 1*"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

I know your sample is just made-up, but what should happen to row 5 if C5 happened to contain "Longstring123"?
I'm asking because in your sample "string1" is always surrounded by spaces or is at the start/end of the cell text
 
Upvote 0
Thanks for your reply, I modified the generic word "string1" in "Scherm", an actual word, but I get an immediate error

I tried with and without the spaces
 

Attachments

  • 1Cattura.PNG
    1Cattura.PNG
    4.4 KB · Views: 6
  • 2Cattura.PNG
    2Cattura.PNG
    5.8 KB · Views: 5
Upvote 0
Could we see a small section at least of your worksheet with XL2BB?

Also note in any case that would not pick up a row if "Scherm" was the first or last word in the cell.
 
Upvote 0
In addition to Peter's comments, is your data in a table, or just a normal range?
 
Upvote 0
the A1 cell was empty, so the error, I had to write something in A1
I specified A1:C65000 and it works

many thanks :)
 
Upvote 0
Good news.

Does it work fast enough? Deleting lots of disjoint rows can be relatively slow and if you do have a large number of rows like that there is a faster way.
If you are interested in that, post back but include more details about whether "Scherm" or whatever the word is could be the first/last word in a cell.
 
Upvote 0

Forum statistics

Threads
1,215,515
Messages
6,125,279
Members
449,220
Latest member
Excel Master

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