+ Reply to Thread
Results 1 to 8 of 8

Remove all rows after Value is found in column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Remove all rows after Value is found in column

    Hey,

    The macro I'm looking for should be fairly simple but everything I've tried isn't working and I don't know what I'm doing wrong. I need a macro to look at the name in: C2 and search from C3 down to end of page, the we only need to know when it first encounters the name. (C7 in my example) and then it needs to delete that row and other rows below .

    So in this example it looks at C2 and finds James Low it searchs and finds James Low on C7 and then deletes the rows from 7 to 21 (A7:D21) (See attachment)

    Thanks in advance
    - Hyflex
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Remove all rows after Value is found in column

    Try this:
    Sub x()
      
    Dim rFind As Range
     
    With Sheet1.Columns(3)
        Set rFind = .Find(What:=.Cells(2), After:=.Cells(2), LookAt:=xlWhole, SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rFind Is Nothing Then
            Range(rFind, .Cells(Rows.Count).End(xlUp)).EntireRow.Delete
        End If
    End With
         
    End Sub

  3. #3
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Remove all rows after Value is found in column

    Awesome, works perfectly.

    Could I possibly ask for a similar macro but instead of looking at C2 it needs to look at B2 and when it needs to go down each cell until B finds a different value when it finds a different value to what is in B2 it deletes that row and all rows below it.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Remove all rows after Value is found in column

    Hi Hyflex,
    see if the following code does what you want.
    Sub DeleteDupsRange()
        Dim LastRow As Double
        Dim RowCtr As Double
        
        LastRow = Cells(Rows.Count, "C").End(xlUp).Row
        
        For RowCtr = 3 To LastRow
            If Cells(2, "C") = Cells(RowCtr, "C") Then
                Rows(RowCtr & ":" & LastRow).Delete
            End If
        Next RowCtr
    End Sub
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Remove all rows after Value is found in column

    So in your example that would leave only row 2?

  6. #6
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Remove all rows after Value is found in column

    Quote Originally Posted by StephenR View Post
    So in your example that would leave only row 2?
    Yeah, the examples quite poor for the secondary macro.

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Remove all rows after Value is found in column

    With apologies to MarvinP...
    Sub DeleteDupsRange()
        Dim LastRow As Double
        Dim RowCtr As Double
        
        LastRow = Cells(Rows.Count, "B").End(xlUp).Row
        
        For RowCtr = 3 To LastRow
            If Cells(2, "B") <> Cells(RowCtr, "B") Then
                Rows(RowCtr & ":" & LastRow).Delete
                Exit Sub
            End If
        Next RowCtr
    End Sub

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Remove all rows after Value is found in column

    Imitation = flattery - OK with me, no apologies needed.

+ 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