Question : Excel 2003 - How to delete rows - based on a criteria - as part of a macro

Hello

I have an Excel 2003 issue.  I want to delete rows based on a criteria.  The criteria being that any row which has a number greater than zero in Column D (e.g.).

The tricky part is, I want to do this as part of a macro.  So basically, if the spreadsheet has 200 rows and only 100 of those rows have a number > zero in Column D, I want the macro to be able to identify the rows that do not meet the criteria, and delete them.  

Can this be done ?  For what it is worth, all of the rows that have a value in Column D are in descending order, therefore, the range that I am wanting to delete will be in a block.

Answer : Excel 2003 - How to delete rows - based on a criteria - as part of a macro


Sub DeleteRows()
Dim ARow, ACol As Integer
    ' Deletes rows from the clicked cell down
    ARow = ActiveCell.Row
    ACol = 4 ' Column D
    Do
        Cells(ARow, ACol).Select
        If Cells(ARow, ACol).Value > 0 Then
            ActiveCell.Rows("1:1").EntireRow.Select
            Selection.Delete Shift:=xlUp
        Else
            ARow = ARow + 1
        End If
    Loop Until IsEmpty(Cells(ARow, 1)) ' Column A is not empty
End Sub

wbr Janos
Random Solutions  
 
programming4us programming4us