+ Reply to Thread
Results 1 to 6 of 6

Search column bottom up then delete rows based on value found

Hybrid View

  1. #1
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Search column bottom up then delete rows based on value found

    I've attached a dummy worksheet that shows constant values of 1.00 in columns D and E. In the actual workbook, user action will sometimes cause these values to change to something other that 1.00. The user may wish to delete the action that caused the change.

    I need a macro that will search from the last cell in column D upward to the first instance of a cell that is not equal to 1.00 and select that cell and those immediately above with the same value as the first found cell.

    For example, in the attached worksheet the search would start in the last cell of Columd D and search upward. It would find cell D23 and upward to D19. These entire rows would be deleted.

    I'm drawing a blank today on how to approach this. Any help would be appreciated.

    Thanks J
    Attached Files Attached Files
    Last edited by jaslake; 07-08-2009 at 10:25 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search column bottom up then delete rows based on value found

    Hello Jaslake,

    This macro, along with a button to call it, have been added to the attached workbook.
    Sub DeleteRows()
    
      Dim Cell As Range
      Dim Rng As Range
      
        Set Rng = Worksheets("Sheet1").Range("D6:D33")
          For Each Cell In Rng
            If Cell.Value <> 1 Then Cell = ""
          Next Cell
        
        On Error Resume Next
          Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search column bottom up then delete rows based on value found

    Almost does what I want. Except that the macro deletes ALL instances of rows that are not equal to 1.00. It should be deleting only the last instance (and it's shared values, in my example 14.00).

    I'll play with it and see what I can do.

    Thanks for your help. J

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search column bottom up then delete rows based on value found

    I just stepped through the macro and it's searching from the top down. I wish to search from the bottom up and select the last instance of the "not 1.00" scenario.

    Any suggestions?

    Thanks J

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search column bottom up then delete rows based on value found

    Hello jaslake,

    Easy enough to change...
    Sub DeleteRows()
    
      Dim R As Long
      Dim Rng As Range
      Dim Test As Variant
      
        Set Rng = Worksheets("Sheet1").Range("D6:D33")
     
        For R = Rng.Rows.Count To 1 Step -1
          Test = Rng.Cells(R, 1).Value
            If Test <> 1 Then
              Do
                Rng.Cells(R, 1) = ""
                R = R - 1
              Loop While Rng.Cells(R, 1) = Test And R <> 0
              Exit For
            End If
        Next R
        
        On Error Resume Next
        Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
    End Sub

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Search column bottom up then delete rows based on value found

    Perfect!

    Thanks Leith. J

+ 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