Macro to delete rows with criteria of "0"

Emb21

Board Regular
Joined
Jul 8, 2004
Messages
152
Hello all,

Still on Excel 2003

My macro is only deleting some of the rows and I dont know why. Note the I have the formating set to currency in M9:M44.

Sub Rectangle7_Click()
ActiveSheet.Unprotect
Dim x As Range
Dim y As Range
Set y = Range("M29:M44")
Application.ScreenUpdating = False

For Each x In y
If x.Value = "0" Then
x.EntireRow.Delete
End If
Next x

ActiveSheet.Protect
End Sub

Any help would be appreciated.

Thanks,
Eric from PA
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Say you delete row 5. Then row 6 will move up to replace it. Then your macro will test if row 6 is zero, but what was row 6 is now row 5, and what was row 7 is now row 6. So when your macro now tests if row 6 is zero, it skips over old row 6 (which is now row 5 because it moved up) and really tests old row 7 (which is now row 6).

You need to test each row from the bottom up.

Code:
Sub Rectangle7_Click()
    
    Dim x As Long
    
    ActiveSheet.Unprotect
    Application.ScreenUpdating = False
    
    For x = 44 To 29 Step -1
        If Range("M" & x).Value = 0 Then Rows(x).Delete
    Next x
    
    Application.ScreenUpdating = True
    ActiveSheet.Protect
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top