VBA Loop to delete rows with 0

U16426

New Member
Joined
Feb 28, 2017
Messages
12
Hi,

I have this vba below. It's designed to delete a row if the value in B is "0" within the given range.

Code:
' Delete Macro
'
 
Dim cell As Range
 
For Each cell In Range("A4:B8")
    If cell.Value = "0" Then
        cell.EntireRow.Delete
    End If
Next cell
'
End Sub

However, it only works in certain situations. For example, it will delete the "Fair" and "Very Good" rows but not the "Excellent" row as well. Any ideas?



Thank you
 

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.
Try
VBA Code:
If cell.Value = "0" Or cell.Value = "Fair" Or cell.Value = "Very Good" Then
 
Upvote 0
Try
VBA Code:
If cell.Value = "0" Or cell.Value = "Fair" Or cell.Value = "Very Good" Then
Sorry my picture didnt add to show my example. The values in Column B will constantly change. So it needs to be dynamic based upon the values placed in Column B
Poor3
Fair0
Good5
Very Good0
Excellent0
 
Upvote 0
Try working from bottom to top.
How many rows of data do you actually have?

VBA Code:
Sub Del_rows()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Range("B" & r).Value = 0 Then Rows(r).Delete
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try working from bottom to top.
How many rows of data do you actually have?

VBA Code:
Sub Del_rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  For r = Range("B" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Range("B" & r).Value = 0 Then Rows(r).Delete
  Next r
  Application.ScreenUpdating = True
End Sub
Thank you. That worked.

It's only 5 rows of data from the table above
 
Upvote 0
How about a no loop solution in case the column gets much larger? :

VBA Code:
 Sub DelRowIfZeroInColumn()
'
    Application.ScreenUpdating = False
'
    Dim StartRowOfRange     As Long
    Dim StartColumnOfRange  As String
'
    StartColumnOfRange = "B"                                                                        ' <--- Set this to the start column of data
    StartRowOfRange = 1                                                                             ' <--- Set this to the start row of data
'
    Range(StartColumnOfRange & StartRowOfRange, Range(StartColumnOfRange & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=0
    Range(StartColumnOfRange & StartRowOfRange + 1, Range(StartColumnOfRange & Rows.Count).End(xlUp)).SpecialCells(12).EntireRow.Delete
'
    If Range(StartColumnOfRange & StartRowOfRange) = 0 Then Rows(StartRowOfRange).EntireRow.Delete
'
    ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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