+ Reply to Thread
Results 1 to 7 of 7

Find Criteria, Delete rows above and below

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    111

    Find Criteria, Delete rows above and below

    I need to search column A for "Brown..." then delete three rows above an four rows below that and loop it until the end of the sheet. Sheet contains 6000 rows. Nothing manual is fast because of gaps between the "Brown..." and i have not bee able to get any VBA Coding to work correctly. I am starting over an looking for some HELP

  2. #2
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Hi Ada01

    I put together this code - crude I know but it seems to work

    Sub Delete_Rows_Based_On_Criteria()
    
    Dim newUpRow, newDownRow As Integer
    
    Range("A1").Select
        Cells.Find(What:="Brown", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        
        newUpRow = ActiveCell.Row - 3
        Rows(newUpRow).Delete shift:=xlUp
        
        newDownRow = ActiveCell.Row + 4
        Rows(newDownRow).Delete shift:=xlUp
    
    End Sub
    Hope this helps

    Seamus

  3. #3
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    111

    RE: Not Deleting??

    The macro won't work, it come close, it seems to be finding, just not deleting...i haven't had any luck with minor adjustments.
    Attached Files Attached Files

  4. #4
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Ada01,

    I looked at your example. Did not realise that there would be many blank cells between each "Brown".

    What do you want to do exactly? Delete the third row above and fourth row below Brown OR delete all three rows above and four rows below Brown?

    The code I posted finds the first instance of "Brown" and then deletes the row three above that row and four rows below that.

    Not sure what you mean by "not coming close"

    Regards

  5. #5
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    Ada01,

    I've looked again and the code below does the following:

    Starts at A1 and looks for "Brown Printing Company" and when it finds it, selects from 3 rows above to 4 rows below and deletes them. It carries on until it returns to A1 again at which point the Sub ends.

    Is that more what you were after?


    Sub Delete_Rows_Based_On_Criteria()
    Dim newUpRow, newDownRow As Integer
    Range("A1").Select
        Cells.Find(What:="Brown Printing Company", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    Do While ActiveCell.Address <> "$A$1"
    Do_Delete
        Cells.FindNext(After:=ActiveCell).Activate
    Loop
    End Sub
    
    Function Do_Delete()
        newUpRow = ActiveCell.Row - 3
        newDownRow = ActiveCell.Row + 4
        Rows(newUpRow & ":" & newDownRow).Select
        Rows(newUpRow & ":" & newDownRow).Delete shift:=xlUp
    End Function
    Regards

  6. #6
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    111

    Works

    I am sorry, i was not able to get back on unitl this morning. the macro worked great, it took me over an 3 hours to do this manually last time to 7 reports, each over 6000 rows.

    Thanks a lot!!

  7. #7
    Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    327
    You're welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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