+ Reply to Thread
Results 1 to 7 of 7

Delete rows in data where column B doesn't meet criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Delete rows in data where column B doesn't meet criteria

    Hi,

    In the attached example, I have a list of Basketball matches in column B and what I want to do is reduce the list (by deleting unwanted rows, not filtering) to matches that just contain NBA matches. The full version of my data will contain thousands of rows so you can imagine this will save me a lot of hard work not manually go through each row.

    In the worksheet 'Team List', I have a list of the team names to verify which matches to keep. Basically, any row in column B that does not contain a team name from my 'Team List' wants to be deleted. There is a couple of teams that look like duplicates in my team list but that's because they have been spelt slightly differently in the original data set. Therefore, it is right to keep both spelling of Cleveland Cavaliers in the Team List.

    Any help, much appreciated.

    Cheers,
    Adam.
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,956

    Re: Delete rows in data where column B doesn't meet criteria

    Quick question...What is the criteria of row delete?
    Must both Teams be found in Team list to be kept or....
    If only one of the teams are found in Team list row must be kept
    If no teams are found then the row be deleted....

    Try this long way....
    Option Explicit
    Sub Delete_Teams()
    Dim i As Long, ii As Long, lRow As Long
    Dim ws As Worksheet, Found As Boolean
    Dim String1 As String, Teams As String, Team1 As String, Team2 As String
    Dim rng As Range, Team As Variant
    
    Application.ScreenUpdating = True
    Set ws = Sheets("Marketing Stats")
    lRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
    For i = lRow To 2 Step -1
        If ws.Cells(i, 2) Like "*NBA Markets" Or ws.Cells(i, 2) = "NCAA: Championship Index" Then GoTo nxt
            String1 = ws.Cells(i, 2)
            Teams = Left(String1, WorksheetFunction.Find(" (", String1, 1) - 1)
            If Teams Like "* v *" Then
                Team1 = Left(Teams, WorksheetFunction.Find(" v ", Teams, 1) - 1)
                Team2 = Mid(Teams, WorksheetFunction.Find(" v ", Teams, 1) + 3)
            Else
                Team1 = Left(Teams, WorksheetFunction.Find(" at ", Teams, 1) - 1)
                Team2 = Mid(Teams, WorksheetFunction.Find(" at ", Teams, 1) + 4)
            End If
            With Sheets("Team List").Range("A:A")
            Team = Array(Team1, Team2)
            For ii = LBound(Team) To UBound(Team)
                Set rng = .Find(What:=Team(ii), After:=.Cells(1), LookIn:=xlValues, _
                                    LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False)
                If Not rng Is Nothing Then
                    Found = True
                Else
                    Found = False
                    ws.Cells(i, 2).EntireRow.Delete
                    GoTo nxt
                End If
            Next ii
        End With
    nxt:
    Next i
    Application.ScreenUpdating = False
    End Sub
    Last edited by Sintek; 07-01-2017 at 08:41 AM. Reason: ScreenUpdating in reverse lol.....Thanks xladept
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: Delete rows in data where column B doesn't meet criteria

    Hi Sintek,

    Thanks for your reply. In answer to your question, both of the teams names in the fixture name in column B, must be present in the 'Team List'.

    I'm not sure if you have accounted for this in your code but when I just tried to run the code, it didn't seem to remove any rows at all.

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,956

    Re: Delete rows in data where column B doesn't meet criteria

    both of the teams names in the fixture name in column B, must be present
    Then try amended code in post 2

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete rows in data where column B doesn't meet criteria

    adam2308

    Try this one.
    Should be very fast.
    Sub test()
        Dim rng As Range, temp, myRng As Range
        With Sheets("team list")
            With .Range("a2", .Range("a" & Rows.Count).End(xlUp))
                temp = .Value
                With .Offset(, 2).Cells(1).Resize(, .Rows.Count)
                    .Value = Application.Transpose(temp)
                    Sheets("marketing stats").Range("b1").Copy .Offset(-1)
                    .Value = .Parent.Evaluate("""<>*""&" & .Address & "&""*""")
                    Set rng = .Offset(-1).Resize(2)
                End With
            End With
        End With
        With Sheets("marketing stats").Cells(1).CurrentRegion
            .AdvancedFilter 1, rng
            .Offset(1).EntireRow.Delete
            .Parent.ShowAllData
        End With
        Sheets("team list").[c1].CurrentRegion.Clear
    End Sub

  6. #6
    Forum Contributor
    Join Date
    02-20-2009
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    467

    Re: Delete rows in data where column B doesn't meet criteria

    perfect jindon - very clever!

    One thing I didn't pick up on is I would also need to delete the rows that have "(Specials)" within the fixture name? Any chance we can add a bit more code to deal with this?

    Thanks.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Delete rows in data where column B doesn't meet criteria

    Try change to
    Sub test()
        Dim rng As Range, temp, myRng As Range
        With Sheets("team list")
            With .Range("a2", .Range("a" & Rows.Count).End(xlUp))
                temp = .Value
                With .Offset(, 2).Cells(1).Resize(, .Rows.Count)
                    .Value = Application.Transpose(temp)
                    Sheets("marketing stats").Range("b1").Copy .Offset(-1)
                    .Value = .Parent.Evaluate("""<>*""&" & .Address & "&""*""")
                    .Cells(2, 1).Value = "*(Specials)*"
                    Set rng = .CurrentRegion
                End With
            End With
        End With
        With Sheets("marketing stats").Cells(1).CurrentRegion
            .AdvancedFilter 1, rng
            .Offset(1).EntireRow.Delete
            .Parent.ShowAllData
        End With
        Sheets("team list").[c1].CurrentRegion.Clear
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 03-18-2015, 11:30 AM
  2. Replies: 7
    Last Post: 10-23-2012, 08:38 AM
  3. How to delete all rows with the same name as a row that meet a certain criteria.
    By shaihulud in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-19-2010, 04:50 PM
  4. Delete rows that do not meet specific criteria
    By SITCFanTN in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2006, 11:36 AM
  5. [SOLVED] Macro, delete rows that meet criteria
    By Scott Wagner in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-22-2005, 08:10 PM
  6. Delete Rows where cells does not meet criteria
    By Danny in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-12-2005, 01:05 PM
  7. how do i delete rows when cells meet certain criteria?
    By Tbal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1