Deletring Rows That Don't Match Criteria

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,080
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
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
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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
 

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,080

ADVERTISEMENT

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?
 

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
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:

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,080

ADVERTISEMENT

yeah thats right
 

glerwell

Well-known Member
Joined
Jun 25, 2006
Messages
1,080
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,580
Messages
5,832,582
Members
430,144
Latest member
bruno92

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
Top