VB SCript to Delete Zero Rows


Posted by Conor Woods on October 25, 2001 6:17 AM

Hi all,

Can anyone help me? new to VBS and trying to get a script that will delete all rows that have all zeros for example:
A B C D
1 2 1 8
0 0 0 0 **
1 4 5 8
0 0 0 0 **
I need to delete these **
Please help if u can!!!

Posted by Damon Ostrander on October 25, 2001 7:28 AM

Hi Conor,

Here is a simple macro that when executed will delete rows filled with zeros on the active worksheet. It figures out on its own how many rows and columns there are and will not delete rows that do not have all zeros out to the last filled column.

Sub DeleteZeroRows()

' This macro deletes all rows on the active worksheet
' that have data cells that are filled with all zeros
' out to some column and are empty thereafter.
' Important: does not delete rows with empty cells

Dim LastCol As Integer
Dim iRow As Long
Dim iCol As Integer
For iRow = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
LastCol = Cells(iRow, 255).End(xlToLeft).Column
For iCol = 1 To LastCol
If Cells(iRow, iCol) <> 0 Or IsEmpty(Cells(iRow, iCol)) Then GoTo SkipRow
Next iCol
Rows(iRow).Delete
SkipRow:
Next iRow
End Sub

If you want it to recognize empty cells as zeros, then just eliminated the

Or IsEmpty(Cells(iRow, iCol))

from the code above. This would then also delete empty rows. Or rows having zeros and empty cells intermixed.

Happy computing.

Damon




Posted by Conor on October 25, 2001 7:51 AM


thanks dude,
works a charm - you the man.
appreciated
Conor