Delete Rows Based on Part of a String Value

kaneda0149

Board Regular
Joined
Aug 4, 2009
Messages
74
I found this fabulous VBA code that delete rows/shift up based on a certain text value. It works great when the cell contains only that specific value but when it's a cell that contains multiple string values and I want to delete based on part of the string in the value.

For example cell text value in column A: "Mickey Mouse Var. Rat"

I want to delete the rows, shift up if any text in column A has in it "Var. Rat". The code below does not work when there are other texts in the cell.

Thanks in advance!

Code:
Sub DeleteSpecific_Rows()
' This macro deletes all rows on the active worksheet
' that have the following text in column A.
 Dim rng As Range, cell As Range, del As Range
 Set rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
 For Each cell In rng
   If (cell.Value) = "Var. Rat" _
    Or (cell.Value) = "VRDN" _
    Or (cell.Value) = "NA" Then
       If del Is Nothing Then
          Set del = cell
       Else: Set del = Union(del, cell)
       End If
    End If
 Next cell
 On Error Resume Next
 del.EntireRow.Delete
End Sub
 
Please post a sample of your data and expected result.!!

For some reason the original answer provided in this thread did not work the first several tries, but finally worked yesterday. Not sure why, but all is well now and the the code is working perfectly. Thank you!
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Apologies for resurrecting this a few years later, however how would one invert this code? Ie keep all rows containing "Var.Rat", but delete all the others?! I have tried swapping the Set del rows, but get a Invalid procedure error on the first Set line...

Any ideas gratefully received!
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG21Jan37
[COLOR="Navy"]Dim[/COLOR] rng [COLOR="Navy"]As[/COLOR] Range, cell [COLOR="Navy"]As[/COLOR] Range, del [COLOR="Navy"]As[/COLOR] Range
 [COLOR="Navy"]Set[/COLOR] rng = Intersect(Range("A:A"), ActiveSheet.UsedRange)
 [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] cell [COLOR="Navy"]In[/COLOR] rng
   [COLOR="Navy"]If[/COLOR] InStr(cell, "Var. Rat") = 0 [COLOR="Navy"]Then[/COLOR]
      [COLOR="Navy"]If[/COLOR] del [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
          [COLOR="Navy"]Set[/COLOR] del = cell
            Else: [COLOR="Navy"]Set[/COLOR] del = Union(del, cell)
          [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
 [COLOR="Navy"]Next[/COLOR] cell
 [COLOR="Navy"]If[/COLOR] Not del [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] del.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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