Deletring Rows That Don't Match Criteria

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,082
Hi

I looking at some past data from last years Premiership. What I want to do is streamline the data that I have, so what I would like to do is delete the rows that don't match a criteria.

The criteria must be in either column B or C otherwise that row is deleted.

eg. the Criteria is "CHELSEA" so any rows that don't contain "CHELSEA" in columns B or C are deleted.

I know it would probably have to be a VBA code to do but I'm don't where to start.

Any help woulb be greatly appreciated!

Cheers
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Put this in your worksheet VBE

Code:
Sub DeleteRows
Dim lr As Integer
 
Application.ScreenUpdating = False
 
lr = Range("A" & Rows.Count).End(xlUp).Row
 
For c = 1 To lr
If Instr(1,Range("B" & c).Value,"CHELSEA") = 0 Then
If Instr(1, Range("C" & c).Value, "CHELSEA") = 0 Then
Row(c).Delete
c = c - 1
End If
End If
Next c
 
Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're going to want to make that go backwards...

Change

For c = 1 To lr
to
For c = lr To 1 Step -1


And

Row(c).Delete
should be
Rows(c).Delete
 
Upvote 0
I just tried this code:

Code:
Sub DeleteRows()
Dim lr As Integer
 
Application.ScreenUpdating = False
 
lr = Range("A" & Rows.Count).End(xlUp).Row
 
For c = lr To 1 Step -1
If InStr(1, Range("B" & c).Value, "CHELSEA") = 0 Then
If InStr(1, Range("C" & c).Value, "CHELSEA") = 0 Then
Rows(c).Delete
End If
End If
Next c

Application.ScreenUpdating = True
End Sub

And all of the data was deleted?
 
Upvote 0
No... It should be right o_O....

So, if CHELSEA is found in column B OR C, you want them to be kept right?
 
Last edited:
Upvote 0
Could this modified so that the criteria is whatever the value of A1 is. This way I could use it with other teams without the need to manually change the code.

So if A1 = "ARSENAL" then all the Arsenals would be left

This would need it to start at A3 though, so that the top two rows are not deleted as they would always be needed.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,292
Members
448,885
Latest member
LokiSonic

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