Delete row if range does not contain string

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
Hi, can someone modify the code below to delete the entire row instead of just the cell where the text does not begin with the word "PARTS". I found this code online so If there is a better way to do this please offer. Thanks in advance.

VBA Code:
Sub testparts()
For Each cl In Range("A44:A47")
    If UCase(Left(cl.Value, 5)) <> "PARTS" Then
        cl.ClearContents
    End If
Next cl
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Do you want to physically delete the row or do what the macro you posted does and simply clear the cells' contents?
 
Upvote 0
Give this a try (change the range in the With statement to match the actual range of cells you want to process)...
VBA Code:
Sub DeleteNonPARTS()
  With Range("A44:A47")
    .Value = Evaluate(Replace("IF(LEFT(@,5)<>""PARTS"",""#N/A"",IF(@="""","""",@))", "@", .Address))
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
 
Upvote 0
Solution
Give this a try (change the range in the With statement to match the actual range of cells you want to process)...
VBA Code:
Sub DeleteNonPARTS()
  With Range("A44:A47")
    .Value = Evaluate(Replace("IF(LEFT(@,5)<>""PARTS"",""#N/A"",IF(@="""","""",@))", "@", .Address))
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
Looks good! Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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