Deleting rows based on a value in that row


Posted by Erik Peterson on May 07, 2001 3:19 PM

I have a problem that I'm sure has a simple answer, but I can't figure it out?!

I have a worksheet, that I've built off of some raw data I import with some macros I built. The macro creates a calculated value.

What I need to do, is automatically delete all the rows that are greater than -1 in a given column.

I'm going crazy trying to figure this out.

Thanks in advance for any help.

- Erik

Posted by CDW on May 07, 2001 4:40 PM

If your column is column A :-

Dim theColumn As Range
Set theColumn = Range(Range("A1"), Range("A65536").End(xlUp))
Columns("A:A").Insert
With theColumn.Offset(0, -1)
.FormulaR1C1 = "=IF(RC[1]>-1,1,"""")"
On Error Resume Next
.SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete
On Error GoTo 0
.EntireColumn.Delete
End With

Posted by Erik Peterson on May 07, 2001 4:59 PM

Thanks, this worked great ! I really appreciate it.

Would you mind adding in a few comments to help explain how you did this ?

Thanks,

Erik



Posted by CDW on May 08, 2001 2:48 AM

declare a variable for your data range
Dim theColumn As Range
'set your data range to be checked for values greater than -1 as A1 to the last cell with data in column A
Set theColumn = Range(Range("A1"), Range("A65536").End(xlUp))
'insert a new blank column before column A
Columns("A:A").Insert
'in this new column (i.e. your data range, one column to the left)
With theColumn.Offset(0, -1)
'enter a formula that will produce 1 if the value in your data range is greater than -1 (otherwise shows nothing)
.FormulaR1C1 = "=IF(RC[1]>-1,1,"""")"
'delete cells that show 1 in the new column ( the error handler is there in case there are no cells that contain 1)
On Error Resume Next
.SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete
On Error GoTo 0
'delete the new column
.EntireColumn.Delete
End With

Actually, you can create a macro to do this, without any knowledge of VBA, by using the macro recorder.
The code produced will be a bit longer and less efficient (and will need to be adjusted slightly to work on any range size in column A rather than a specific range), but have a look at the code from the macro recorder based on the actions listed below.
This wiil probably give you a better understanding of the code. It is also a useful learning process in developing VBA knowledge/skills.

1.Select column A
2.Insert>Columns
3.Select cells from A1 to the end of the data in column B
4.Type the formula =IF(B1>-1,1,"") and press Ctrl+Enter
5.Edit>GoTo>Special>Formulas>Numbers(de-select Text/Logicals/Errors)
6.Edit>Delete>EntireRow
7.Select Column A
8.Edit>Delete

(This is probably the easiest way to do it if you want to do it "manually" without a macro - but I'm not prepared to bet on it!)

There are other ways to write code to achieve the same result. For example, code could be written to loop through each cell in your data range and delete the appropriate rows. However, if your data range is large, code that uses a loop will probably take noticeably longer to run than the code proposed above.
Have fun!