For Each Loop Deleting Row if Cell = 0

excel, vba

Solution

When deleting, the typical approach is to start at the bottom and loop up. This requires an index-specified loop, rather than FOR EACH. Here are some details:

For I=TotalRows To 1 Step -1 
    Set c = Range("C" & I) 
     ' code to check for criteria and delete if required
Next I 

taken from here: http://www.ozgrid.com/forum/showthread.php?t=56516

Problem

I'm trying to write a macro that will delete a row if a cell = 0 in the range given. The problem I am coming across is when the For Each Loop runs it will find the cell and delete the row but if the row below it also had a 0 it ends up getting skipped by the code since the code has moved onto the next range. I'm looking to have a macro that will find 0 in a range of cells and will loop on that range that had a 0 until the that cell is greater than 0. I've got this as a work in progress... ``` Sub Pub_Clean() Dim IRange As Range Dim VRange As Range Set VRange = Range(ActiveSheet.Range("b3"), ActiveSheet.Range("b3").End(xlDown)) For Each IRange In VRange If IRange = 0 Then IRange.EntireRow.Delete End If Next IRange End Sub ```

Original source