+ Reply to Thread
Results 1 to 20 of 20

delete rows when column has specific word(s)

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    delete rows when column has specific word(s)

    Super noob

    I have been trying to delete specific word in say column H say the word springfield

    have tried a few scripts but it deletes data from column F

    please help
    here is what I have

    Sub Macro1()
    
      Dim FoundIt As Range
      Dim Rng As Range
      Dim SearchWord As String
      
        SearchWord = "ABC"
        Set Rng = Columns("B:B")
        
          Set FoundIt = Rng.Find(What:=SearchWord, _
                                 After:=Rng.Cells(1, 1), _
                                 LookIn:=xlValues, _
                                 LookAt:=xlWhole, _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlNext, _
                                 MatchCase:=False)
                                 
          If Not FoundIt Is Nothing Then
             Set FoundIt = FoundIt.Resize(Rowsize:=5)
             FoundIt.EntireRow.Delete
          End If
             
    End Sub

    thanks in advance for any direction/help
    Last edited by alansidman; 09-21-2013 at 10:42 PM. Reason: Added code tags

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: delete rows when column has specific word(s)

    So you need to find all cells with the word "ABC" in column "B" and if its found then delete the whole row. Correct?

    Thanks

  3. #3
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: delete rows when column has specific word(s)

    Try this is a copy of your workbook. Data will be delete and It cannot be undone.

    Sub DeleteRows()
    
    Dim lrow As Long
    Dim i As Long
    
    lrow = Cells(Rows.Count, "B").End(xlUp).Row
    
    Application.ScreenUpdating = False
        For i = lrow To 1 Step -1
            If Cells(i, "B").Value = "ABC" Then
                Cells(i, "B").EntireRow.Delete
            End If
        Next i
    Application.ScreenUpdating = True
    
    End Sub
    Thanks

  4. #4
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    Thanks Fredlo2010

    didn't work but also no error

    Thanks again

  5. #5
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: delete rows when column has specific word(s)

    Quote Originally Posted by chrisblackman View Post
    didn't work but also no error
    It works for me. Most likely its just a reference issue. Can you attach a small sample of your workbook?

    Thanks

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,706

    Re: delete rows when column has specific word(s)

    Chris - Welcome to the forum, however........

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added the code tags for you. Please comply with our rules in the future)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  7. #7
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    My apologies and will do in the future

    Quote Originally Posted by alansidman View Post
    Chris - Welcome to the forum, however........

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added the code tags for you. Please comply with our rules in the future)

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: delete rows when column has specific word(s)

    The code you have works too, but need few tweaking.

    Sub Macro1()
    
      Dim FoundIt As Range
      Dim Rng As Range
      Dim SearchWord As String
      
        SearchWord = "ABC"
        Set Rng = Columns("H:H")  'This is column where the code searches
        
          Set FoundIt = Rng.Find(What:=SearchWord, _
                                 After:=Rng.Cells(1, 1), _
                                 LookIn:=xlValues, _
                                 LookAt:=xlWhole, _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlNext, _
                                 MatchCase:=False)
                                 
          If Not FoundIt Is Nothing Then
             'Set FoundIt = FoundIt.Resize(Rowsize:=5) ' if the aim is to delete entire row, no need for this line, but it may  not be 
             FoundIt.EntireRow.Delete
          End If
             
    End Sub

  9. #9
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    Thanks for your help but I am unable to post attachment
    I'm following instructions but don't see where to attach


    I'm sorry

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,706

    Re: delete rows when column has specific word(s)

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: delete rows when column has specific word(s)

    Did you try post # 8?

  12. #12
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    I did and nothing happened

    Quote Originally Posted by AB33 View Post
    Did you try post # 8?

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: delete rows when column has specific word(s)

    Try the attached.
    The code was initially written for a loop code ,hence you see lots of commenting on some of the lines.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    AB33 Thanks very much!!! it works

    Quote Originally Posted by AB33 View Post
    Try the attached.
    The code was initially written for a loop code ,hence you see lots of commenting on some of the lines.

  15. #15
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    Thanks a million

    here is attachment
    I am trying to delete rows where column H has certain word say "longmeadow"
    or any word specific word that I need rows removed
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: delete rows when column has specific word(s)

    hi, chrisblackman, option, please check attachment, run code "test" or press Run button
    Attached Files Attached Files

  17. #17
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: delete rows when column has specific word(s)

    This one works and deletes

    Sub Macro1()
    
      Dim FoundIt As Range
      Dim Rng As Range
      Dim SearchWord As String
      
        SearchWord = "longmeadow"
        Set Rng = Columns("H:H")  'This is column where the code searches
        
          Set FoundIt = Rng.Find(What:=SearchWord, _
                                 After:=Rng.Cells(1, 1), _
                                 LookIn:=xlValues, _
                                 LookAt:=xlWhole, _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlNext, _
                                 MatchCase:=False)
                                 
          If Not FoundIt Is Nothing Then
             'Set FoundIt = FoundIt.Resize(Rowsize:=5) ' if the aim is to delete entire row, no need for this line, but it may  not be
             FoundIt.EntireRow.Delete
          End If
             
    End Sub

  18. #18
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: delete rows when column has specific word(s)

    Here is a variant you can also try.

    Sub DeleteRows()
    
    Dim lrow As Long
    Dim i As Long
    Dim n As Long
    
    'Enter all the values to delete below
    Keywords = Array("longmeadow") '<-----Enter the words to look for here inside "" and separated by commas
    
    lrow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Application.ScreenUpdating = False
    For n = 0 To UBound(Keywords)
        For i = lrow To 1 Step -1
            If UCase(Cells(i, "H").Value) = UCase(Keywords(n)) Then
                Cells(i, "H").EntireRow.Delete
            End If
        Next i
    Next n
    Application.ScreenUpdating = True
    
    End Sub
    Thanks

  19. #19
    Registered User
    Join Date
    09-21-2013
    Location
    portland,maine
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: delete rows when column has specific word(s)

    Fredlo2010 Thanks so very much it works also

  20. #20
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: delete rows when column has specific word(s)

    You are welcome

+ 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] Macro to delete rows that DO NOT contain specific word
    By enfieldsteve in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-19-2013, 10:59 AM
  2. [SOLVED] Need a macro to delete rows with specific word
    By renjithvakkayil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-07-2013, 12:54 PM
  3. Delete rows or Clear contents below a specific word is found in Column A
    By kishoremcp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2012, 04:08 PM
  4. Delete all rows that dont contain a specific word.
    By BPSJACK in forum Excel General
    Replies: 4
    Last Post: 03-15-2012, 05:05 AM
  5. Macro to look for a specific word and delete rows
    By excelaspire0219 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-27-2009, 06:35 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