+ Reply to Thread
Results 1 to 7 of 7

MACRO for Deleting rows based on a cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    03-19-2018
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    27

    MACRO for Deleting rows based on a cell value

    HI there - hoping someone can help. I am building a macro and in it I would like to delete all rows that have "DELETE ROW" in column AB. Sounds simple but I can't find a solution anywhere. Can anyone help? PLEASE!

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: MACRO for Deleting rows based on a cell value

    How about:

    Sub del_row()
    
    Dim ws As Worksheet
    Dim lr As Long
    
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'change sheet name as needed
    lr = ws.Range("A" & Rows.Count).End(xlUp).Row 'last row with data, using column A, change if needed
    ws.Range("AB1:AB" & lr).AutoFilter field:=1, Criteria1:="DELETE ROW"
    ws.Range("AB2:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ws.AutoFilterMode = False
    
    End Sub
    Last edited by Arkadi; 03-23-2018 at 02:29 PM. Reason: Changed the red 2, it used to be 1 but that deletes the header row.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    03-19-2018
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    27

    Re: MACRO for Deleting rows based on a cell value

    I am not sure what I am doing wrong, this is how I have the macro into VBA

    Sub del_row()
    
    Dim ws As Worksheet
    Dim lr As Long
    
    Sheets("B291-Inventory Report by Locati").Select
    lr = ws.Range("A" & Rows.Count).End(xlUp).Row
    ws.Range("AB1:AB" & lr).AutoFilter field:=1, Criteria1:="DELETE ROW"
    ws.Range("AB2:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ws.AutoFilterMode = False
    
    End Sub
    Last edited by jeffreybrown; 03-23-2018 at 02:54 PM. Reason: Please use code tags!

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

    Re: MACRO for Deleting rows based on a cell value

    Or another option...

    Sub FilterDelete()
        With Range("AB1", Range("AB" & Rows.Count).End(xlUp))
            .AutoFilter Field:=1, Criteria1:="DELETE ROW"
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
    End Sub
    HTH
    Regards, Jeff

  5. #5
    Registered User
    Join Date
    03-19-2018
    Location
    Chicago
    MS-Off Ver
    2016
    Posts
    27

    Re: MACRO for Deleting rows based on a cell value

    jeffreybrown,

    this worked - thank you so much!!!
    Last edited by jeffreybrown; 03-23-2018 at 02:50 PM. Reason: Removed full quote

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: MACRO for Deleting rows based on a cell value

    When you used my code, you activated the sheet, but did not set the WS object to the sheet, so "ws" in the code had no meaning. You simply needed

    Set ws = ThisWorkbook.Worksheets("B291-Inventory Report by Locati")
    at the start.
    Last edited by Arkadi; 03-23-2018 at 03:01 PM. Reason: Removed moderation request.

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: MACRO for Deleting rows based on a cell value

    Thanks for fixing your post (#4)

    If you are happy with the solution and the issue is resolved, please mark the thread as solved when you have a moment?

    Thanks in advance, and happy coding

    EDIT: I should also have taken a moment to say welcome to the forum! I believe you will find lots of people eager to be helpful when you have the need for it.
    Last edited by Arkadi; 03-23-2018 at 03:06 PM.

+ 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. Macro: Deleting Rows based on date value
    By hreal34 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2012, 02:08 PM
  2. Replies: 4
    Last Post: 01-20-2011, 03:39 AM
  3. Macro for deleting rows based on criteria from another sheet
    By arnab0711 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2011, 03:21 AM
  4. Replies: 1
    Last Post: 01-19-2011, 05:08 AM
  5. Deleting rows based on condition:help me create a macro
    By chrisjack001 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-08-2010, 07:34 PM
  6. deleting rows based on cell value
    By cart0250 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2006, 07:19 AM
  7. Deleting rows based on a cell value
    By JPriest in forum Excel Formulas & Functions
    Replies: 37
    Last Post: 09-06-2005, 07:05 PM

Tags for this Thread

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