+ Reply to Thread
Results 1 to 5 of 5

How do I delete all rows containing a certain text string? / rows containg x, y. or z

Hybrid View

  1. #1
    Registered User
    Join Date
    12-06-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2007
    Posts
    31

    How do I delete all rows containing a certain text string? / rows containg x, y. or z

    Hi -- found a good script for this, but I need to delete rows that contain different variables and I don't know how to run that several times at one go - fr example delete all lines containing Fred,Paul, or Jim

    This was the original script in the previous reply to another members question: --- it requires input fr the string, but I would prefer knowing how I could just embed the terms. Thanks in advance for help on this

    Sub deleteRows()
        Dim rng    As Range
        Dim rng2   As Range
        Dim cl     As Range
        Dim str    As String
    
        str = InputBox("Enter search text", "Find & Delete")
        If str = "" Then Exit Sub
        Set rng = ActiveSheet.UsedRange
        For Each cl In rng
            If cl.Value Like "*" & str & "*" Then
                If rng2 Is Nothing Then
                    Set rng2 = cl
                Else: Set rng2 = Union(rng2, cl)
                End If
            End If
        Next cl
        rng2.EntireRow.Delete
    Moderator's Note: When posting codes. Select the code then hit "#" sign to enclose the codes with code tags. Thank you and welcome to the forum.
    Last edited by vlady; 12-18-2012 at 07:57 PM. Reason: code tags

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: How do I delete all rows containing a certain text string? / rows containg x, y. or

    Hi

    Try this

    Sub deleteRows()
        Dim rng    As Range
        Dim rng2   As Range
        Dim cl     As Range
        Dim str    As String
    
        'str = InputBox("Enter search text", "Find & Delete")
        'If str = "" Then Exit Sub
        Set rng = ActiveSheet.UsedRange
        For Each cl In rng
            If InStr(1, cl.Value, "fred") > 0 Or InStr(1, cl.Value, "paul") > 0 Or InStr(1, cl.Value, "jim") > 0 Then
                If rng2 Is Nothing Then
                    Set rng2 = cl
                Else: Set rng2 = Union(rng2, cl)
                End If
            End If
        Next cl
        rng2.EntireRow.Delete
      End Sub
    rylo

  3. #3
    Registered User
    Join Date
    12-06-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How do I delete all rows containing a certain text string? / rows containg x, y. or

    hey many thanks --- is there a tweak though so that for example "Paul" row would be deleted but it would not also delete "Paula" and "Apauling" -- but this gets me most of the way ----

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: How do I delete all rows containing a certain text string? / rows containg x, y. or

    Hi

    If the words are surrounded by spaces, then include the spaces in the string. So for jim it would be
    instr(1,cl.value, " jim ")
    If it isn't likely to be contained within spaces, then what combinations would you be likely to expect? " jim" or "jim " perhaps?

    rylo

  5. #5
    Registered User
    Join Date
    12-06-2012
    Location
    Amsterdam
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: How do I delete all rows containing a certain text string? / rows containg x, y. or

    gets that's basic --- thanks ---

    Just posted what I think will be my last part of the project I am working through

    "assistance in changing data in one column based on another" if you got the time and inclination --- Phillip

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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