Macro to delete rows if cell contains string
Closed
jonny578
Posts
1
Registration date
Friday February 23, 2018
Status
Member
Last seen
February 23, 2018
-
Feb 23, 2018 at 09:29 PM
vcoolio Posts 1411 Registration date Thursday July 24, 2014 Status Moderator Last seen September 6, 2024 - Feb 24, 2018 at 07:50 AM
vcoolio Posts 1411 Registration date Thursday July 24, 2014 Status Moderator Last seen September 6, 2024 - Feb 24, 2018 at 07:50 AM
Related:
- Excel macro delete row if cell contains value
- If cell contains date then return value ✓ - Office Software Forum
- Excel conditional formatting if cell contains specific text - Excel Forum
- Count if cell contains number - Excel Forum
- Saints row 2 cheats - Guide
- Delete my whatsapp account without app - Guide
1 response
vcoolio
Posts
1411
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
September 6, 2024
262
Updated on Feb 24, 2018 at 08:11 AM
Updated on Feb 24, 2018 at 08:11 AM
Hello Jonny,
A relatively simple method to do the task for you would be to use a helper column to extract the string "ABC" from the entire string in any cell in Column D then filter the helper column for the extracted string "ABC" and then delete the relevant rows.
Following is the code that should do the task:-
The code firstly inserts a formula (line 8 in the code above) in Column I (the helper column in this case) which extracts "ABC" from the string in Column D.
The code then filters for "ABC" in Column I and deletes the relevant rows of data. The helper column is then cleared.
As I don't know the set out of your worksheet, following is the link to a sample that I have prepared for you so you can see how it works.
http://ge.tt/4J8NKio2
The sample has a few rows of data with only three that don't contain "ABC" in Column D so only three rows of data should be left once filtering/deleting is completed. Click on the "RUN" button to see it work.
I hope that this helps.
Cheerio,
vcoolio.
A relatively simple method to do the task for you would be to use a helper column to extract the string "ABC" from the entire string in any cell in Column D then filter the helper column for the extracted string "ABC" and then delete the relevant rows.
Following is the code that should do the task:-
Sub DeleteIt() Dim lr As Long Application.ScreenUpdating = False lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row Sheet1.Range("I2:I" & lr) = "=IF(D2="""","""",IF(ISNUMBER(SEARCH(""ABC"",D2,1)),""ABC""))" With Sheet1.[A1].CurrentRegion .AutoFilter 9, "ABC" .Offset(1).EntireRow.Delete .AutoFilter End With Sheet1.Columns(9).ClearContents Application.ScreenUpdating = True End Sub
The code firstly inserts a formula (line 8 in the code above) in Column I (the helper column in this case) which extracts "ABC" from the string in Column D.
The code then filters for "ABC" in Column I and deletes the relevant rows of data. The helper column is then cleared.
As I don't know the set out of your worksheet, following is the link to a sample that I have prepared for you so you can see how it works.
http://ge.tt/4J8NKio2
The sample has a few rows of data with only three that don't contain "ABC" in Column D so only three rows of data should be left once filtering/deleting is completed. Click on the "RUN" button to see it work.
I hope that this helps.
Cheerio,
vcoolio.