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
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,565
Messages
5,512,091
Members
408,879
Latest member
dilipsny

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top