+ Reply to Thread
Results 1 to 11 of 11

Delete all rows which doesn't contain certain text criteria (various criteria)

  1. #1
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Delete all rows which doesn't contain certain text criteria (various criteria)

    Hi all,

    Can somebody more knowledgeable than me please provide me with the code that I need for the following:

    I am looking for help concerning deleting various rows not containing certain criteria using VBA.

    I receive a particular spreadsheet at work each week which has to be broken up into various 'case types'. I want to be able to set up some code which I can copy and paste and put into VBA to make my life easier.
    - The spreadsheet is around 300 rows long, and the criteria I need to filter out is in column A
    - The criteria I need to group together all have different text, for example EGSA001 & AGTO003 belong together to filter out. So I would want to delete all other rows not containing this information.
    - So I need a code which allows me to specify several different criteria (50+) to delete all other rows not containing this information.

    I have nearly got there before with different bits of code but the sheer amount of different criteria to filter out has been too long and caused me issues.

    Thanks,

    EB
    Last edited by ellbeardo; 10-07-2020 at 08:53 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Consider using Excel's Advanced Filter feature.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Try something like this. Remember to delete from the bottom up.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Hi, the problem with the filter is that I still have to filter them all out manually each week.

    I'm just looking for some code I can set up and copy and paste into each week's file to delete all the rows except the ones I want.

    Thanks

  5. #5
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Quote Originally Posted by maniacb View Post
    Try something like this. Remember to delete from the bottom up.

    Please Login or Register  to view this content.
    Hi thanks but it doesn't look like that one works, I have managed to delete some rows before but the problem comes when I need to insert around 60/70 versions of 'EGSA001' or 'AGTO003' into the code and I run out of space on the line.

    I have managed to use the following below which doesn't run out of space, but it deletes the criteria instead of keeping the criteria stated. Any ideas on how to change this code to keep the rows - not delete them?

    Sub Example1()

    Dim varList As Variant
    Dim lngarrCounter As Long
    Dim rngFound As Range, rngToDelete As Range
    Dim strFirstAddress As String

    Application.ScreenUpdating = False

    varList = VBA.Array("AAPC001", "DLPC001", "PCAA608", "PCAA609", "PCAA620", "PCAA621", "PCAA623", "PCAA624", "PCAA650", "PCAA651", "PCAA652", "PCCA608", "PCDB601", "PCDB602", "PCDL608", "PCDL609", "PCDL616", "PCDL617", "PCDL620", "PCDL621", "PCDL623", "PCDL624", "PCDL625", "PCDL650", "PCDL651", "PCDL652", "PCDL653", "PCES602", "PCES604", "PCES605", "PCHB001", "PCIC601", "PCIC602", "PCIC603", "PCIC604", "PCIC607", "PCIP604", "PCPC608", "PCPC611", "PCPC612", "PCPC613", "PCPC614", "PCPC615", "PCPC617", "PCPC618", "PCPD601", "PCPD602", "PCPI019", "PCPI609", "PCPI619", "PCPI623", "PCPI625", "PCPI650", "PCPI653", "PCPS601", "PCPS602", "PCPS603", "PCRM601", "PCRM602", "PCRP619", "PCRP620", "PCRP621", "PCRP623", "PCRP624", "PCRP625", "PCRP630", "PCRP631", "PCRP633", "PCRP634", "PCRP637", "PCRP638", "PCRP639", "PCWD601", "PCWD602")
    For lngarrCounter = LBound(varList) To UBound(varList)

    With Sheets("RP YTD").UsedRange
    Set rngFound = .Find( _
    What:=varList(lngarrCounter), _
    Lookat:=xlWhole, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, _
    MatchCase:=True)

    If Not rngFound Is Nothing Then
    strFirstAddress = rngFound.Address

    If rngToDelete Is Nothing Then
    Set rngToDelete = rngFound
    Else
    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
    Set rngToDelete = Application.Union(rngToDelete, rngFound)
    End If
    End If

    Set rngFound = .FindNext(After:=rngFound)

    Do Until rngFound.Address = strFirstAddress
    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
    Set rngToDelete = Application.Union(rngToDelete, rngFound)
    End If
    Set rngFound = .FindNext(After:=rngFound)
    Loop
    End If
    End With
    Next lngarrCounter

    If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete

    Application.ScreenUpdating = True

    End Sub

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-14-2020 at 12:51 AM.

  7. #7
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    Hi Alpha,

    I'm afraid that just deleted the whole of the worksheet contents!

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    It worked for me! Attach an example worksheet!

  9. #9
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Quote Originally Posted by AlphaFrog View Post
    It worked for me! Attach an example worksheet!
    Not sure why I tried it a few times on mine!

    I've finally cracked it anyway this did the job of what I needed to do...

    Sub DeleteRows()
    Dim List As Variant
    Dim LR As Long
    Dim r As Long
    List = Array("AAPC001", "DLPC001", "PCAA608", "PCAA609", "PCAA620", "PCAA621", "PCAA623", "PCAA624", "PCAA650", "PCAA651", "PCAA652", "PCCA608", "PCDB601", "PCDB602", "PCDL608", "PCDL609", "PCDL616", "PCDL617", "PCDL620", "PCDL621", "PCDL623", "PCDL624", "PCDL625", "PCDL650", "PCDL651", "PCDL652", "PCDL653", "PCES602", "PCES604", "PCES605", "PCHB001", "PCIC601", "PCIC602", "PCIC603", "PCIC604", "PCIC607", "PCIP604", "PCPC608", "PCPC611", "PCPC612", "PCPC613", "PCPC614", "PCPC615", "PCPC617", "PCPC618", "PCPD601", "PCPD602", "PCPI019", "PCPI609", "PCPI619", "PCPI623", "PCPI625", "PCPI650", "PCPI653", "PCPS601", "PCPS602", "PCPS603", "PCRM601", "PCRM602", "PCRP619", "PCRP620", "PCRP621", "PCRP623", "PCRP624", "PCRP625", "PCRP630", "PCRP631", "PCRP633", "PCRP634", "PCRP637", "PCRP638", "PCRP639", "PCWD601", "PCWD602")
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For r = LR To 1 Step -1
    If IsError(Application.Match(Range("A" & r).Value, List, False)) Then
    Rows(r).Delete
    End If
    Next r
    End Sub

  10. #10
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Thanks for the help by the way Alpha.

  11. #11
    Registered User
    Join Date
    10-07-2020
    Location
    Nottm, England
    MS-Off Ver
    365
    Posts
    33

    Re: Delete all rows which doesn't contain certain text criteria (various criteria)

    Quote Originally Posted by ellbeardo View Post
    Not sure why I tried it a few times on mine!

    I've finally cracked it anyway this did the job of what I needed to do...

    Sub DeleteRows()
    Dim List As Variant
    Dim LR As Long
    Dim r As Long
    List = Array("AAPC001", "DLPC001", "PCAA608", "PCAA609", "PCAA620", "PCAA621", "PCAA623", "PCAA624", "PCAA650", "PCAA651", "PCAA652", "PCCA608", "PCDB601", "PCDB602", "PCDL608", "PCDL609", "PCDL616", "PCDL617", "PCDL620", "PCDL621", "PCDL623", "PCDL624", "PCDL625", "PCDL650", "PCDL651", "PCDL652", "PCDL653", "PCES602", "PCES604", "PCES605", "PCHB001", "PCIC601", "PCIC602", "PCIC603", "PCIC604", "PCIC607", "PCIP604", "PCPC608", "PCPC611", "PCPC612", "PCPC613", "PCPC614", "PCPC615", "PCPC617", "PCPC618", "PCPD601", "PCPD602", "PCPI019", "PCPI609", "PCPI619", "PCPI623", "PCPI625", "PCPI650", "PCPI653", "PCPS601", "PCPS602", "PCPS603", "PCRM601", "PCRM602", "PCRP619", "PCRP620", "PCRP621", "PCRP623", "PCRP624", "PCRP625", "PCRP630", "PCRP631", "PCRP633", "PCRP634", "PCRP637", "PCRP638", "PCRP639", "PCWD601", "PCWD602")
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For r = LR To 1 Step -1
    If IsError(Application.Match(Range("A" & r).Value, List, False)) Then
    Rows(r).Delete
    End If
    Next r
    End Sub

    Does anybody know how I can extend the Array line on this? It lets me put about 85 of my criteria on a single line before it doesn't let me put anymore in. Is there a way of carrying on on the next line down.

    Thanks,

+ 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. Delete rows in data where column B doesn't meet criteria
    By adam2308 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-01-2017, 11:58 AM
  2. Delete Rows Based On A Text Criteria
    By NSTurk725 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-24-2014, 12:22 AM
  3. Delete rows which do not meet specific criteria (text values)
    By dim06 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2012, 09:54 AM
  4. Sorting and Delete rows meeting criteria based on sorting criteria
    By ron2k_1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2011, 02:40 PM
  5. Delete rows based on multiple criteria (Date & Text)
    By Poulan in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 10-08-2010, 09:00 AM
  6. Formula to delete column if it doesn't match criteria
    By stekng1fan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-02-2009, 02:39 PM
  7. Auto Filter Delete Rows by Criteria Doesn't Work Range To Complicated
    By robertjtucker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-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