How to remove entire rows that contains a range of words

nicoan

New Member
Joined
Apr 6, 2013
Messages
25
Hello guys

Say I have the following sheet:

Column A
blue car
green car
red car
cyan car
magenta car
black car
yellow car
brown car
white car
violet car

The sheet have content in the column B and C irrelevant to this case. And in column D I have a range of words like this:

Column D
blue
violet
black

I need the cells content on the column D to be used as filters to strip all the rows (from A, B and C) that contains them. So the result will be:

Column A
green car
red car
cyan car
magenta car
yellow car
brown car
white car


I hope is clear enough. Can you please help?

Thanks in advance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try setting up this example:
On Sheet1
A1: Description and A2:A11 contains your list
Description
blue car
green car
red car
cyan car
magenta car
black car
yellow car
brown car
white car
violet car



On Sheet2
A1: Words and A2:A4 contains your list of words
Words
blue
violet
black

B1: Description <---This must match the column heading on Sheet1
B2: ="*"&A2&"*"

Copy that formula down through B4.
They will display this way:
Description
*blue*
*violet*
*black*

Note: The asterisks are wildcards that match anything before and after the text

Back to Sheet1
• Select A1:A11
• Data.Advanced_filter....If prompted: Click: OK
...List Range: $A$1:$A$11 <---this will already be selected
...Criteria Range: <--Click the Sheet2 tab and select B1:B4
...Click: OK

• Select the visible cells (excluding the heading)...so you'll be selecting A2:A11..which actually selects A2, A7, A11
• Home.Delete.Delete_sheet_rows
• Data.Clear_filter

The end result will be:
Description
green car
red car
cyan car
magenta car
yellow car
brown car
white car


Is that something you can work with?
 
Upvote 0
nicoan,

Welcome to the MrExcel forum.

Sample raw data in worksheet Sheet1 (the raw data in columns A, B, and C, can have a different number of rows):


Excel 2007
ABCD
1blue carblack caryellow carblue
2green carblue carwhite carviolet
3red carbrown carviolet carblack
4cyan carcyan carred car
5magenta cargreen carmagenta car
6black carmagenta cargreen car
7yellow carred carcyan car
8brown carviolet carbrown car
9white carwhite carblue car
10violet caryellow carblack car
11
Sheet1


After the macro:


Excel 2007
ABCD
1green carbrown caryellow carblue
2red carcyan carwhite carviolet
3cyan cargreen carred carblack
4magenta carmagenta carmagenta car
5yellow carred cargreen car
6brown carwhite carcyan car
7white caryellow carbrown car
8
9
10
11
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub RemoveSearchStrings()
' hiker95, 04/06/2013
' http://www.mrexcel.com/forum/excel-questions/695653-how-remove-entire-rows-contains-range-words.html
Dim a As Variant, b As Variant, s As Variant, ss As Long
Dim i As Long, ii As Long, iii As Long, c As Long, lr As Long
With Sheets("Sheet1")
  lr = .Cells(Rows.Count, 4).End(xlUp).Row
  s = .Cells(1, 4).Resize(lr).Value
  For c = 1 To 3 Step 1
    lr = .Cells(Rows.Count, c).End(xlUp).Row
    a = .Cells(1, c).Resize(lr)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    iii = 0
    For i = 1 To UBound(a, 1)
      ss = 0
      For ii = 1 To UBound(s, 1)
        If InStr(a(i, 1), s(ii, 1)) > 0 Then
          ss = ss + 1
        End If
      Next ii
      If ss = 0 Then
        iii = iii + 1
        b(iii, 1) = a(i, 1)
      End If
    Next i
    .Cells(1, c).Resize(UBound(b, 1)) = b
    Erase a: Erase b
  Next c
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the RemoveSearchStrings macro.
 
Upvote 0
Thank you both very much for your replies.

Hiker95, the macro seems to work partially, as the columns B and C seems to be untouched. Now the cells from column A doesn´t correspond with the rest of the columns.
In your example seems to work because the filter words are also there. But column B and C have a completely different content.

Can you please fix it so that it removes the whole rows (excepting the filter column)?

Thanks again.
 
Upvote 0
nicoan,

So that we can get it right the next time, can we have a screenshot of the raw data in columns A, B, C, D , and, a screenshot of what the results should look like (manually formatted by you).

To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker

If you are not able to give us screenshots:
You can upload your workbook to Box Net,
sensitive data scrubbed/removed/changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
nicoan,

I think I now understand what you are trying to accomplish.

Sample raw data in worksheet Sheet1:


Excel 2007
ABCD
1blue carblue carblue carblue
2green cargreen cargreen carviolet
3red carred carred carblack
4cyan carcyan carcyan car
5magenta carmagenta carmagenta car
6black carblack carblack car
7yellow caryellow caryellow car
8brown carbrown carbrown car
9white carwhite carwhite car
10violet carviolet carviolet car
11
Sheet1


After the updated macro:


Excel 2007
ABCD
1green cargreen cargreen carblue
2red carred carred carviolet
3cyan carcyan carcyan carblack
4magenta carmagenta carmagenta car
5yellow caryellow caryellow car
6brown carbrown carbrown car
7white carwhite carwhite car
8
9
10
11
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub RemoveSearchStringsV2()
' hiker95, 04/06/2013
' http://www.mrexcel.com/forum/excel-questions/695653-how-remove-entire-rows-contains-range-words.html
Dim a As Variant, b As Variant, s As Variant, ss As Long
Dim i As Long, ii As Long, iii As Long, lr As Long
With Sheets("Sheet1")
  lr = .Cells(Rows.Count, 4).End(xlUp).Row
  s = .Cells(1, 4).Resize(lr).Value
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  a = .Cells(1, 1).Resize(lr, 3)
  ReDim b(1 To UBound(a, 1), 1 To 3)
  For i = 1 To UBound(a, 1)
    ss = 0
    For ii = 1 To UBound(s, 1)
      If InStr(a(i, 1), s(ii, 1)) > 0 Then
        ss = ss + 1
      End If
    Next ii
    If ss = 0 Then
      iii = iii + 1
      b(iii, 1) = a(i, 1)
      b(iii, 2) = a(i, 2)
      b(iii, 3) = a(i, 3)
    End If
  Next i
  .Cells(1, 1).Resize(UBound(b, 1), UBound(b, 2)) = b
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the RemoveSearchStringsV2 macro.
 
Upvote 0
nicoan,

Thanks for the feedback.

You are very welcome. Glad I could help.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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