Deleting entire rows given a condition

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

PincivMa

Thread Starter
Joined
Mar 13, 2004
Messages
378
Hi There

I have created a macro that deletes entire rows when it meets a certain condition. The condition is as follows:

The macro starts in Cell G2. In column G, if the Active Cell is > 730 then delete the entire row. Else If the Active Cell <= 730 then go down to next cell. Some cells in column G are blank. They have no data. I have over 55, 000 rows of data and the macro that I did takes forever to run since it checks every single cell. I stops when the last cell in column A is a blank.

Is there a macro that can do this quickly? In case you are interested below is the macro that I wrote.

Range("G2").Select
Do
ActiveCell.Select
If ActiveCell > 730 Then
ActiveCell.EntireRow.Delete
ElseIf ActiveCell <= 730 Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until ActiveCell.Offset(0, -6) = ""


Mario
 
Joined
Jun 17, 2002
Messages
2,556
Can you sort your Data prior to running the Delete? This will move all the empty cells to the bottom...then it will stop when it hits the empty cell
 
Joined
Jul 1, 2005
Messages
8,546
Try another approach.

1. Build formulas in all the rows in the first free column. If first free column is H, then:

=IF(G2>730,1,"X")

would give you 1 for 740, X for blank and X for 730.

You could do this with one code line:

Range("A2", Range("A" & Rows.Count).End(xlUp)).Offset(, 7).FormulaR1C1 = "=IF(RC[-1]>730,1,""X"")"

(note: this will build the formulas in column H)

For my 55000 test rows, it takes < 2 seconds to execute.

The next step would be:

Range("H2", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeFormulas, 1).Select

, followed by deleting the selected rows ... and then column H.

HTH (make sure your work is "safe" before you try anything :) )
 
Joined
Jul 1, 2005
Messages
8,546
Also, re: "takes forever" -- possibly you have loads of formulas & it's recalculating the entire sheet every time you delete a row. Consider toggling automatic calculation off and on.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top