VBA to Delete rows with Zero values Loop

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
160
I have search previous threads and have tried different coding to delete the entire row of data if the value in column M (12) is zero to no avail. Getting very frustrated as this should be pretty simple coding. I must have a syntax error somewhere as it will only delete one row at a time when I run the macro.

Sub CleanSPS()
'
' CleanSPS Macro
'
Dim SPS As Worksheet
Set SPS = ActiveWorkbook.Sheets("SPS+")

Dim LastRowSPS As Long
LastRowSPS = SPS.Cells(Rows.Count, 12).End(xlUp).Row

Dim p As Long ' for projection
'Delete rows that contain a projection value of "0" in column L

For p = 3 To LastRowSPS

If Cells(p, 12).Value = 0 Then
Cells(p, 12).EntireRow.Delete
End If

Next p

MsgBox "Finished"

End Sub


Can you see my error?
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
When deleting rows you need to loop backwards. try
Code:
For p = LastRowSPS To 3 Step -1
   
   If SPS.Cells(p, 12).Value = 0 Then
      SPS.Rows(p).Delete
   End If

Next p
 
Last edited:
Upvote 0
Your code is designed to delete one row at a time because your loop counter is increment sequentially +1 each time
Rich (BB code):
For p = 3 to LastRow
If Cells(p, 12).Value = 0 Then Cells(p, 12).EntireRow.Delete
Next p
Part in red is a single cell with p being a variable that increases sequentially (i.e. +1) so it is going to delete one row at a time. It's probably doing this wrong and should be:
Rich (BB code):
For p = LastRow to 3 Step-1
If Cells(p, 12).Value = 0 Then Cells(p, 12).EntireRow.Delete
Next p

This is one suggestion to delete in a single call:
Rich (BB code):
With Cells(3, 12).Resize(LastRow - 2)
    .Replace 0, ""
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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