+ Reply to Thread
Results 1 to 4 of 4

Macro to delete rows based off criteria?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2014
    Location
    toronto
    MS-Off Ver
    2010
    Posts
    16

    Macro to delete rows based off criteria?

    I'm not sure if this is possible, but I would like to know if the following is achievable..


    I have a spreadsheet that outputs many rows that look like the following:
    Capture.PNG

    I am looking for a macro to link to a button that when pressed:

    every row that contains the word "Good" in column B is deleted as well as the row above it deleted as well.
    So for example, if the button is pressed, rows 359 & 360 from the picture above would be deleted.


    Thank you!

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Macro to delete rows based off criteria?

    Sub Reset()
    
        Dim i As Integer
        Dim LastRow As Integer
        
        Application.ScreenUpdating = False
        
        LastRow = Worksheets(ActiveSheet.Name).Cells(Rows.Count, 1).End(xlUp).Row  'This will find the last used row in column A
    
            For i = LastRow To 2 Step -1
                If i <> 2 Then 'Skips Second Row
                    If UCase(Cells(i, 2).Value) = "GOOD" Then 'Makes the word uppercase to avoid problems because good <> GOOD in VBA
                        Rows(i - 1 & ":" & i).EntireRow.Delete 'delete the row with GOOD and the one above it.
                            i = i - 1 'Since we deleted two rows, we need to move the counter an additional spot
                    End If
                End If
            Next i
    
        Application.ScreenUpdating = True
        
    End Sub
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro to delete rows based off criteria?

    Sub Select_Select()
       
        Set rngLook = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row)
    
        With rngLook
            Set rngFind = .Find("Good", .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = Union(rngFind.Offset(-1, 0), rngFind)
                Do
                    Set rngPicked = Union(rngPicked, rngFind.Offset(-1, 0), rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
        rngPicked.EntireRow.Delete
    
        End If
    
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  4. #4
    Registered User
    Join Date
    12-17-2014
    Location
    toronto
    MS-Off Ver
    2010
    Posts
    16

    Re: Macro to delete rows based off criteria?

    Thanks all for the replies! I will follow up when I get a chance to try out the macros tomorrow.

+ 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 to delete rows based on two different criteria matching
    By Cpower17 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2014, 05:43 PM
  2. [SOLVED] Macro to delete rows based on multiple criteria
    By Andrew E Smith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2014, 12:17 PM
  3. Macro to delete rows based on criteria in 2 maybe 3 fields
    By russellh79 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-31-2014, 03:46 PM
  4. macro - delete rows based on multiple criteria.
    By ChocksterNo1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2013, 10:22 AM
  5. How can I delete rows based on specific criteria using a macro?
    By akouleze in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-24-2011, 02:49 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