Consulting

Results 1 to 5 of 5

Thread: Delete all rows after....

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location

    Delete all rows after....

    I am trying to delete all rows after a certain cell has a certain value. For example, in Column A, I have the following 10 rows:
    1
    2
    3
    4
    Last Row
    Not Needed
    7
    8
    9
    10

    I am looking to delete all rows (or clear the contents) starting with the one that has the "Not Needed" in it, so I only want to keep rows 1 through "Last Row".

    Thank you!

  2. #2
    Try this

    [vba]Sub test()
    Dim rng As Range
    Set rng = Range("A:A").Find("Not Needed", , xlValues, xlPart, , , False)
    If rng Is Nothing Then
    MsgBox "String 'Not Needed' was not found."
    Exit Sub
    Else
    Set rng = Range(rng, rng.End(xlDown))
    rng.EntireRow.Delete
    End If
    End Sub
    [/vba]
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Or try this:
    [vba]
    Sub Macro2()
    Dim FndCell As String
    Dim Rng As String
    FndCell = Cells.Find(What:="Not Needed", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Address
    Rng = Range("A65536").End(xlUp).Address
    Range(Rng & ":" & FndCell).EntireRow.Delete (xlUp)

    End Sub
    [/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    Sorry, this is not my thread but can you explain Simon this part of your code "Range(Rng & ":" & FndCell)."

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Certainly!, this
    Range(Rng & ":" & FndCell)
    is the same as writing
    Range("A1:B20")
    where A1 would be the Rng i set in this statement
    Rng = Range("A65536").End(xlUp).Address
    i Declared In Memory the name Rng as a String because i used the suffix .Address at the end, FndCell is the name i had given to the results of the FIND function which is also a string, the : is used as in a normal range statement .

    Hope this helps.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •