+ Reply to Thread
Results 1 to 8 of 8

Delete entire row for specific words

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Delete entire row for specific words

    Hello everybody

    I need a macro that enables me to select a range using inputbox ...
    As for selection I need to use (Intersect) method to focus on column D in my selection ...
    I need to put about seven words in an array , e.g (Cat1,Cat2,Cat3,Cat4,Cat5,Cat6,Cat7) ...

    If any cell in column D in that selection only not the whole column :: if any cell has any string of the array the macro delete the entire row

    Hope it is clear
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Delete entire row for specific words

    Hello YasserKhalil,

    See if this works for you :

    Sub DeleteRows()
        Dim rg As Range
        Dim ar As Variant
        Dim i As Long
        
        Application.ScreenUpdating = False
    
        Set rg = Application.InputBox("Select range", , , , , , , 8)
    
        Set rg = Intersect(rg, Columns("D"))
        If rg Is Nothing Then MsgBox "Invalid selection": Exit Sub
    
        ar = InputBox("Enter words separated by comma")
        ar = Split(ar, ",")
    
        'Search words, delete rows
        With rg.Offset(-1, 0).Resize(rg.Rows.Count + 1, 1)
            For i = LBound(ar) To UBound(ar)
                .AutoFilter 1, ar(i)
                On Error Resume Next
                rg.SpecialCells(xlCellTypeVisible).EntireRow.Delete
                On Error GoTo 0
                .AutoFilter
            Next i
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Delete entire row for specific words

    Hi GC Excel,

    First - I really like your code, but I wondered about these things.

    1. What if the user selects cells that don't intersect with column D? You give an error message so that is ok.
    2. Was the question if the whole cell is "Cat1" or if "Cat1" is anywhere in the string in that cell?

    Still - I like your code and learned from it. But I interpreted the question with the points above included.
    If I wrote my code again, I'd add the Input string of words he is looking for. I liked that. We might also include a prompt for which column he is searching for the input words.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,470

    Re: Delete entire row for specific words

    Suggest you post a sample workbook. You should know this by now. Don't expect people to make a test environment before preparing a solution for you.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Delete entire row for specific words

    or maybe so
    Sub ttt()
    Dim rng As Range, v
    On Error Resume Next: Err.Clear
    Set rng = Application.InputBox("select a range", Type:=8)
    If Err Then Exit Sub
    
    With Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns(4), rng)
        For Each v In Array("Cat1", "Cat2", "Cat3", "Cat4", "Cat5", "Cat6", "Cat7")
            .Replace "*" & v & "*", "True", xlWhole
        Next
        .SpecialCells(2, 4).EntireRow.Select
    '    .SpecialCells(2, 4).EntireRow.Delete
    End With
    If Err Then MsgBox "not found needed data", 64
    End Sub

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,359

    Re: Delete entire row for specific words

    Hi Yasser,

    Here is your code:
    Sub ForYasserKhalil()
        Dim CellRng As Range
        Dim BottomRow As Double
        Dim TopRow As Double
        Dim RowCtr As Double
        Dim WordList As Variant
        Dim ListCtr As Double
        
        WordList = Split("Cat1,Cat2,Cat3,Cat4,Cat5", ",")
        
        Set CellRng = Application.InputBox("Select a range of cells", Type:=8)
        ' Type:=8 lets you select a range of cells on the worksheet
        
        TopRow = CellRng.Cells.Row
        BottomRow = CellRng.Cells.Rows.Count + TopRow - 1
    
        For ListCtr = 0 To UBound(WordList)
            For RowCtr = BottomRow To TopRow Step -1
                If InStr(Cells(RowCtr, "D"), WordList(ListCtr)) > 0 Then
                    Cells(RowCtr, "D").EntireRow.Delete Shift:=xlUp
                    BottomRow = BottomRow - 1
                End If
            Next RowCtr
        Next ListCtr
    End Sub
    You will need to select a range of cells by clicking on the worksheet and dragging the mouse. This is Type:=8 input.
    You should also look at the VBA code and add any "Cat" words you want searched for.

    Hope this is what you wanted.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Delete entire row for specific words

    Thanks a lot for this great and useful help
    You are awesome

    All the solutions offered are wonderful
    Thank you very much for this great and brilliant methods
    Last edited by YasserKhalil; 08-01-2015 at 03:08 PM.

  8. #8
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Delete entire row for specific words

    @Marvin,

    Thanks for the feedback...

    2. Was the question if the whole cell is "Cat1" or if "Cat1" is anywhere in the string in that cell?
    You are correct. I should have write this instead since the search value can be anywhere in the cell:

    .AutoFilter 1, "*" &  ar(i) & "*"

+ 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. [SOLVED] keyword search - a macros to change font color for specific key words (not entire cell)
    By kingwhopper in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-10-2014, 12:25 PM
  2. [SOLVED] Macro delete rows containing specific words
    By mcranda in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2013, 05:14 PM
  3. [SOLVED] delete entire row if it doesn't contains at least 1 of the words listed
    By 4evra in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-30-2013, 02:14 PM
  4. Delete blank row above cells with specific words.
    By beyre in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-17-2012, 02:17 AM
  5. [SOLVED] Delete entire row if the cell has three 3 or two 2 letter words
    By plasma33 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2012, 08:23 PM
  6. Delete entire row from A to F if cell A has specific content
    By vtech in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-02-2010, 06:01 AM
  7. Delete a Row if a cells contains specific words
    By jsolder in forum Excel General
    Replies: 1
    Last Post: 08-14-2009, 05:36 AM

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