Likes Likes:  0
Results 1 to 8 of 8

Thread: Sort by value then delete rows underneath

  1. #1
    New Member
    Join Date
    Sep 2018
    Posts
    48
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Sort by value then delete rows underneath

    I have a column with either a value of 1 or blank. The sheet has a header. Is there code that can sort by value in column "Z", find the last row with a value, then just delete all blank rows underneath?

    Ty!!

  2. #2
    Board Regular My Aswer Is This's Avatar
    Join Date
    Jul 2014
    Posts
    13,506
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    6 Thread(s)

    Default Re: Sort by value then delete rows underneath

    So in column Z you have either empty cells or cell with the number 1
    And you want to sort the column and delete all blank rows below
    Well if we sorted the column all the 1's would come to the top.

    So if you now have 45 1's in column Z rows 1 to 45 you want to delete all 1.5 million Rows below the 1's

    Is that what you want?
    I think that is impossible and why do you want to do that.
    Last edited by My Aswer Is This; Oct 3rd, 2018 at 06:10 PM.
    Be sure and always test this script on sample data the first time you use this as to avoid any problems with your data. Always trying to learn more and help others where I can. I'm using Excel 2013.
    Patience please Iím not perfect yet. "Memories are forever"

  3. #3
    New Member
    Join Date
    Sep 2018
    Posts
    48
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Sort by value then delete rows underneath

    Basically, I need to delete rows if there is not a value of 1 in column Z.. and I'd like to do that, without running a code that continuously loops. I tried it a different way and on 25k+ rows, it takes forever.

  4. #4
    Board Regular Michael M's Avatar
    Join Date
    Oct 2005
    Location
    South Western NSW
    Posts
    15,832
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Sort by value then delete rows underneath

    so you are saying that there is data in the other columns below the last row in col Z ??
    If so, which columns will contain extra data ?
    Regards
    Michael M
    ---------------------------------------
    The more I learn, the less I seem to know.....A Please and Thank You cost nothing !
    It's easier to debug if we can see the whole macro !
    Home 2007 & 2013

    - Posting guidelines, forum rules and terms of use

    - To download Mr Excel HTML Maker

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes


    [CODE]Place Your Code Here[/CODE]

  5. #5
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    21,930
    Post Thanks / Like
    Mentioned
    10 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Sort by value then delete rows underneath

    How about

    Code:
    Range("Z:Z").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

  6. #6
    New Member
    Join Date
    Sep 2018
    Posts
    48
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Sort by value then delete rows underneath

    Quote Originally Posted by Michael M View Post
    so you are saying that there is data in the other columns below the last row in col Z ??
    If so, which columns will contain extra data ?
    So basically, my spreadsheet has 5 tabs on it. There is a main tab and then 4 sub tabs. The macro creates the 4 sub tabs if there is relevant data (if sum of Z:Z > 0, subsequent tab is created).

    It then copies all data from the main tab to the sub tabs. There are rows of data that are irrelevant on the sub tabs (where the column Z does not have a value of 1). This is why these columns need to be deleted. I tried just copying over line by line based on column Z = 1, but that proved inefficient. There can be up to 100k lines of data on these tabs, so I need a quick way to eliminate the bad stuff.


    To answer your question though, data may be on columns A through Y, for example. But, for these records, if z <> 1, then I need that row deleted. That's why I thought a quick sort by 1, then delete rows beneath would prove to be the most efficient way to accomplish this.

  7. #7
    Board Regular Michael M's Avatar
    Join Date
    Oct 2005
    Location
    South Western NSW
    Posts
    15,832
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Sort by value then delete rows underneath

    Try using the previously provided code with modifications to suit.

    Code:
    Sub MM1()
    Application.ScreenUpdating = False
    With Columns(26)
        .AutoFilter field:=1, Criteria1:=">" & 1
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
    End Sub
    Regards
    Michael M
    ---------------------------------------
    The more I learn, the less I seem to know.....A Please and Thank You cost nothing !
    It's easier to debug if we can see the whole macro !
    Home 2007 & 2013

    - Posting guidelines, forum rules and terms of use

    - To download Mr Excel HTML Maker

    - Try searching for your answer first, see how

    - Read the FAQs

    - List of BB codes


    [CODE]Place Your Code Here[/CODE]

  8. #8
    New Member
    Join Date
    Sep 2018
    Posts
    48
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Sort by value then delete rows underneath

    Thanks Michael. I took mikerickson's code and applied it.. which seemed to work without even using a sort function (YAY!). I applied your code and it appeared to delete my test file headers. I am curious.. what's the difference between the two, and why would MM1 be preferable?

    Thanks again for all of your help in this thread, and my several other ones.

    Quote Originally Posted by Michael M View Post
    Try using the previously provided code with modifications to suit.

    Code:
    Sub MM1()
    Application.ScreenUpdating = False
    With Columns(26)
        .AutoFilter field:=1, Criteria1:=">" & 1
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
    End Sub

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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