Need To Delete Rows Based On Multiple Criteria

Andyronon

New Member
Joined
Dec 20, 2019
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi, I'm knew to this and would appreciate any help provided. I read somewhere that there is a limit to the number of terms that one macro could handle. I need a VBA script to delete multiple rows in column “A” based on if the cell has (ANY) reference of over 150 terms.

For example, if I want to delete the row that has the word “Ball” in it, three of the below five entries would be deleted.
Ball game
Sports Stadium
Softball game
Football game
Sports Fan


All that would remain is “Sports Stadium” and “Sports Fan”. I tried this script but got error messages

Sub Removal()
Dim i As Long, searchString As String

For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
' if a row is marked for deletion, delete it and continue.
If Left(Range("A" & i), 1) = "0" Then
Rows(i).Delete
' skip to next row
GoTo NextRow
End If

searchString = LCase(Range("A" & i))

If (InStr(1, searchString, "ball") > 0) Or _
(InStr(1, searchString, "number") > 0) Or _
(InStr(1, searchString, "minimum") > 0) Then
Rows(i).Delete
End If


Can someone help with this? What am I doing wrong?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi & welcome to MrExcel.
The easiest way is to create a list of the values you want to delete & then use a formula to find the rows you don't want.
You can then use the autofilter to filter greater than 0 on that column to delete the rows.

For simplicity I've put it on the same sheet, but the list can go on another sheet
Book1
ABCI
1District
2Eden0West
3High Peak1North
4Bradford0east
5Staffordshire Moorlands0South
6Derbyshire Dales0high
7Cheshire East1low
8Calderdale0
9Oldham0
10West Somerset1
11Shropshire0
12County Durham0
13Newcastle-under-Lyme0
14Northumberland1
15Rossendale0
Mon
Cell Formulas
RangeFormula
B2:B15B2=SUM(--ISNUMBER(SEARCH($I$2:$I$7,A2)))
 
Upvote 0
Hi, I tried the formula but it isn’t working for me. I’m seeing a couple of problems and I will list them here.

1. When I enter a word in column “I” the number changes beside a different word in my column “A” list.

2. When I enter a word in column “I” the number only changes beside ONE instance of a word that appears in column “A”……(The number changes beside the wrong word). The words in my A column will be multiple entries. I the code to review the entire list and eliminate the correct word from the entire list. My lists can be 70,000 long.

I tried the solution out on a much smaller sample. Please see attached graphic.
 

Attachments

  • Spreadsheet.png
    Spreadsheet.png
    57.7 KB · Views: 32
Upvote 0
The part of the formula in red
=SUM(--ISNUMBER(SEARCH($I$2:$I$7,A2)))
Should only look at the list of values, you cannot have any blank cells in the range, so as you are only testing one value in that test the formula should be
=SUM(--ISNUMBER(SEARCH($I$2:$I$2,A2)))
and if you had values from I2 down to I100 you would use
=SUM(--ISNUMBER(SEARCH($I$2:$I$100,A2)))
 
Upvote 0
Use an array to delete multiple terms at once.

VBA Code:
Sub DeleteRowsWithText()
    Dim LRow As Long
    Dim x As Long
    Dim Arr As Variant
    Dim i As Long

'Macro Optimization
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

    Arr = Array("*ball*", "*Y*", "*Z*")
    LRow = Cells(Rows.Count, "A").End(xlUp).row
    For x = LRow To 1 Step -1
        For k = LBound(Arr) To UBound(Arr)
            If Cells(x, "L").Value Like Arr(k) Then
                Rows(x).Delete
            End If
        Next k
    Next x

'Turn off Macro Optimization
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Wit over 150 terms to search for, that is likely to be very slow.
 
Upvote 0
I agree. But both solutions are going to take some time to do. The macro will automate it. However if he is counting each word with "ball" (football, soccerball, softball) then yes he is looking for 150 different words. But if he wants to delete anything with ball then he can put wildcards around the word and rock on. That would make the process a lot easier.
 
Upvote 0

Forum statistics

Threads
1,224,739
Messages
6,180,676
Members
452,993
Latest member
FDARYABEE

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