Sort by value then delete rows underneath

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
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!!
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
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:

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
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.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,953
Office Version
2013
Platform
Windows
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 ?
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,619
How about

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

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
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.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,953
Office Version
2013
Platform
Windows
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
 

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
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. :)

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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,047
Messages
5,484,393
Members
407,438
Latest member
DKrakken

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top