+ Reply to Thread
Results 1 to 16 of 16

Delete row if cell contains the word "Warning"

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Question Delete row if cell contains the word "Warning"

    I just want to use VBA code to delete a row if a cell within that row contains the word "Warning"

    Thanks!
    Last edited by CC268; 01-12-2017 at 04:59 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    I have a code here.
    http://www.xlorate.com/excel-vba-loo...e%20to%20Range

    Edit to suite your requirements.


    Sub SelectA1()
        Dim FrstRng As Range, LstRw As Long, s As String
        Dim UnionRng As Range
        Dim c As Range
        
        LstRw = Cells(Rows.Count, "A").End(xlUp).Row
        Set FrstRng = Range("A1:A" & LstRw)
        s = InputBox("What to Find?")
        
        For Each c In FrstRng.Cells
            If c = s Then
                If Not UnionRng Is Nothing Then
                    Set UnionRng = Union(UnionRng, c)    'adds to the range
                    'MsgBox UnionRng.Address    'remove later
    
                Else
                    Set UnionRng = c
                End If
            End If
        Next c
    
        UnionRng.EntireRow.Delete
    End Sub

  3. #3
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Re: Delete row if cell contains the word "Warning"

    Is there a way to do this without the input box...I just want it to look for the word Warning within the excel sheet and delete the row it is in.
    Last edited by jeffreybrown; 01-12-2017 at 05:47 PM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    Edited code.

    Not fair that you edited your question.

    I have a code here.
    http://www.xlorate.com/excel-vba-loo...e%20to%20Range

    Edit to suite your requirements.


    Sub SelectA1()
        Dim FrstRng As Range, LstRw As Long, s As String
        Dim UnionRng As Range
        Dim c As Range
        
        LstRw = Cells(Rows.Count, "A").End(xlUp).Row
        Set FrstRng = Range("A1:A" & LstRw)
        s = "Warning"
        
        For Each c In FrstRng.Cells
            If c = s Then
                If Not UnionRng Is Nothing Then
                    Set UnionRng = Union(UnionRng, c)    'adds to the range
                    'MsgBox UnionRng.Address    'remove later
    
                Else
                    Set UnionRng = c
                End If
            End If
        Next c
    
        UnionRng.EntireRow.Delete
    End Sub

  5. #5
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Re: Delete row if cell contains the word "Warning"

    I got an run time error "91":

    Object variable or With block variable not set.

    When I press the Debug button it highlights the last line "UnionRng.EntireRow.Delete"
    Last edited by jeffreybrown; 01-12-2017 at 05:47 PM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    Okay, you get the error after you run the code a second time, because there are no more rows to delete.

  7. #7
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Re: Delete row if cell contains the word "Warning"

    No..unfortunately it isn't deleting anything at all.
    Last edited by jeffreybrown; 01-12-2017 at 05:48 PM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    Quote Originally Posted by CC268 View Post
    No..unfortunately it isn't deleting anything at all.
    Where are the words? the code searches column A

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    Here is an error trap.

    Sub SelectA1()
        Dim FrstRng As Range, LstRw As Long, s As String
        Dim UnionRng As Range
        Dim c As Range
    
        LstRw = Cells(Rows.Count, "A").End(xlUp).Row
        Set FrstRng = Range("A1:A" & LstRw)
        s = "Warning"
    
        For Each c In FrstRng.Cells
            If c = s Then
                If Not UnionRng Is Nothing Then
                    Set UnionRng = Union(UnionRng, c)    'adds to the range
                    'MsgBox UnionRng.Address    'remove later
    
                Else
                    Set UnionRng = c
                End If
            End If
        Next c
        If Not UnionRng Is Nothing Then
            UnionRng.EntireRow.Delete
        End If
    End Sub

  10. #10
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Re: Delete row if cell contains the word "Warning"

    Hmm...ran it twice...still nothing. If it matters the Warning message is in a merged cell...but I don't think that should matter.
    Last edited by jeffreybrown; 01-12-2017 at 05:48 PM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.

  11. #11
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    You may have to supply a sample.

  12. #12
    Registered User
    Join Date
    03-01-2016
    Location
    Phoenix, AZ
    MS-Off Ver
    MS Office 2013
    Posts
    94

    Re: Delete row if cell contains the word "Warning"

    Quote Originally Posted by davesexcel View Post
    You may have to supply a sample.
    Alright I attached the sheet...I deleted all the data but at least you can see the layout and where the Warning message is (all the way at the bottom). I know it probably just makes sense to scroll to the bottom and delete it manually, but if I can automate it that would be nice as I am doing a lot of other VBA on this sheet.
    Attached Files Attached Files

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Delete row if cell contains the word "Warning"

    Hello CC268,

    In your Post#1, you initially stated a different requirement, hence the suggestions of an Input Box, from the contributors to your Thread.

    Contributors to this Forum spend their time and money to assist others with a specific issue related to their field of expertise, free of charge, and without expecting any recognition for their effort/s. They would be glad to at least receive some sort of comment, by also Adding to their Reputation, by clicking on the star at the bottom left of their Post/s.

    In future, please take a moment to exactly determine your required issue. before posting.

    Thank you for your understanding.

    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  14. #14
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Delete row if cell contains the word "Warning"

    Hello CC268,

    Administrative Note:
    • As per Forum Rule #12, please limit quotes to only that which is necessary -- otherwise it's just clutter...Thanks.
    HTH
    Regards, Jeff

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Delete row if cell contains the word "Warning"

    In post #1 you suggest that the word "Warning" could be anywhere in a row.
    In your example there is no cell with the word "Warning". There is "Warning" as part of a whole lot more in one cell

    If the cell you want to delete is always at the bottom, just clear or delete the last row or cell

    Cells(RowsCells(.Count, 1).End(xlUp).ClearContents
    or
    Rows.Count, 1).End(xlUp).EntireRow.ClearContents
    Or you could change Dave's code to
    Sub SelectA2()
        Dim FrstRng As Range, LstRw As Long, s As String
        Dim UnionRng As Range
        Dim c As Range
    
        LstRw = Cells(Rows.Count, "A").End(xlUp).Row
        Set FrstRng = Range("A1:A" & LstRw)
        s = "WARNING"
    
        For Each c In FrstRng.Cells
            If Left(c, 7) = s Then
                If Not UnionRng Is Nothing Then
                    Set UnionRng = Union(UnionRng, c)    'adds to the range
                    'MsgBox UnionRng.Address    'remove later
    
                Else
                    Set UnionRng = c
                End If
            End If
        Next c
        If Not UnionRng Is Nothing Then
            UnionRng.EntireRow.ClearContents    '<----- or delete
        End If
    End Sub
    Also note search words spellings. Capital letters or small letters.
    Last edited by jolivanes; 01-12-2017 at 07:56 PM. Reason: Add info

  16. #16
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Delete row if cell contains the word "Warning"

    It looks like you will just have the word once, you can find that word and delete the row



    Sub Button1_Click()
    
        Dim ws As Worksheet
        Dim c As Range, FindS As String
    
        Set ws = Worksheets(1)
    
        FindS = "*Warning*"
    
        With ws
            Set c = .Cells.Find(what:=FindS, lookat:=xlWhole)
    
            If Not c Is Nothing Then
                c.EntireRow.Delete
            Else: MsgBox "Not Found"
                Exit Sub
            End If
        End With
    
    End Sub
    I am going to assume you need to delete that row in a bunch of worksheets.

    Sub Loop_Sheets()
    
        Dim ws As Worksheet
        Dim c As Range, FindS As String
    
        FindS = "*Warning*"
    
        For Each ws In Sheets
    
            With ws
    
                Set c = .Cells.Find(what:=FindS, lookat:=xlWhole)
    
                If Not c Is Nothing Then
                    c.EntireRow.Delete
                Else:    'MsgBox "Not Found"
                    'Exit Sub
                End If
    
            End With
    
        Next ws
    
    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. [SOLVED] Delete a cell and shift left if it contains the word "school"
    By clundqui in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-08-2016, 03:27 PM
  2. [SOLVED] Need to Find All Cells Containing "Word" and Replace With "Other Word" from Cell Below?
    By notsmart in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-05-2016, 09:00 AM
  3. Replies: 12
    Last Post: 06-12-2014, 02:11 PM
  4. [SOLVED] Formula Needed to fill multiple cells with "No" when the word "No" is entered into a cell
    By excelteam777 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-09-2013, 05:36 PM
  5. [SOLVED] Macro to delete entire row if cell contains the word "total" or "Total"
    By theatricalveggie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-10-2011, 12:38 PM
  6. Replies: 2
    Last Post: 08-02-2011, 06:57 AM
  7. Replies: 2
    Last Post: 02-27-2006, 04:45 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