Delete rows depending on cell value in range

Rupert Bennett

Active Member
Joined
Nov 20, 2002
Messages
271
Is there a routine one could use to delete any row in a range if the cell value in a specific column meets a certain criterion? I have over 17,000 rows of data but would like to delete all rows where the value in column "D" is zero or less.
Thanks for your help
RB
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Does this code do what you want?

Code:
Sub eliminate_rows()
Application.ScreenUpdating = False
Application.Calculation = xlManual
For x = Range("D65536").End(xlUp).Row To 1 Step -1 'you have to do it from bottom to top, else not eveything will be deleted
If Range("D" & x) <= 0 Then Range("D" & x).EntireRow.Delete
Next x
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub

Regards,
Erik
 
Upvote 0
This might be quicker :-

Code:
Sub eliminate_rows()
Dim rng As Range
Application.ScreenUpdating = False
Columns(4).Insert
Set rng = Range([D1], [E65536].End(xlUp)(1, 0))
With rng
    .FormulaR1C1 = "=IF(RC[1]<=0,1,"""")"
    .Copy
    .PasteSpecial Paste:=xlValues
    .EntireRow.Sort Key1:=rng(1), Order1:=xlDescending, Header:=xlNo
    On Error Resume Next
    .SpecialCells(xlCellTypeConstants, 1).EntireRow.Delete
    On Error GoTo 0
    .EntireColumn.Delete
End With
End Sub
 
Upvote 0
PaddyD said:
not forgetting to change screen updating back to true on the way out...
won't excel automatically do that upon exiting the sub though? at least that's the impression i was under.
 
Upvote 0
Ponsy,

very intresting code
and many times quicker !!!
I'm searching for such a "global" approach for a long time. Although it's not me who startes this thread: THANK YOU !

Rupert will be glad.

Erik
 
Upvote 0
My thanks to everyone for helping. These are great routines which do exactly what I am looking for. My problem is solved.
RB
 
Upvote 0
Zack,
For what it's worth, I've never had a problem with the ScreenUpdating failing to turn itself back on either. (In Office 98 or 2000.) But I've been told that not all versions of Excel will do this and that it's just a good practice to turn back on anything you turn off for your routine, so I usually try to remember to do it. (I've already got enough bad habits writing code like a caveman as it is. I don't need to add that one too.) :unsure:
Dan

Edit: There you go I guess. Corroborated by Juan himself.
 
Upvote 0
thanks Juan/HalfAce,

i remember reading it now on my home machine help files. alas, that was xl 2000. my work pc is xl xp. thanks for the correction though. kind of an odd function to take out :unsure: then again, i'm a far cry from a descent *code* guy :wink:

thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top