Deleting Rows with a specific Value

AlexMach

New Member
Joined
Apr 6, 2009
Messages
17
Hi All,

I have a problem trying to delete multiple rows with the value 0.

I have encode specific rows with either a 0 or 1 whereby the rows with 1's are needed though the ones in between that may have a 0 need deleting (entire row). The the entire data range does not exceed 360 as it's always a full year of data.

This is what I have

Public Sub Delete1()
Dim cell As Range
Range("ad2:ad360").Select
For Each cell In Selection
If cell.Value = "0" Then
cell.EntireRow.Delete
End If
Next cell
Range("ad2").Select
End Sub

the problem is that it is not deleting rows and getting stuck.

Would anyone have a solution to my problem? I would really appreciate any input here.

thanks in advance
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Public Sub Delete2()
Application.ScreenUpdating = False
Dim cell As Range
Range("ad2:ad360").Select
For Each cell In Selection
If cell.Value = "0" Then
cell.EntireRow.Delete
End If
Next cell
On Error Resume Next
 Call Delete2
Range("ad2").Select
End Sub
 
Upvote 0
Hi All,

I have a problem trying to delete multiple rows with the value 0.

I have encode specific rows with either a 0 or 1 whereby the rows with 1's are needed though the ones in between that may have a 0 need deleting (entire row). The the entire data range does not exceed 360 as it's always a full year of data.

This is what I have

Public Sub Delete1()
Dim cell As Range
Range("ad2:ad360").Select
For Each cell In Selection
If cell.Value = "0" Then
cell.EntireRow.Delete
End If
Next cell
Range("ad2").Select
End Sub

the problem is that it is not deleting rows and getting stuck.

Would anyone have a solution to my problem? I would really appreciate any input here.

thanks in advance

Try making this change:

Code:
Public Sub Delete1()
Dim cell As Range
Range("ad2:ad360").Select
For Each cell In Selection
If cell.Value = 0 Then      <--------
cell.EntireRow.Delete
End If
Next cell
Range("ad2").Select
End Sub
 
Upvote 0
Hi! John Davis your code isn't correct erik.van.geit was right visit here http://www.mrexcel.com/forum/showthread.php?t=550847

Thanks for pointing that out. I only made a modification too the ops code. I certainly wouldn't argue with Erik or Richard. However, when I delete rows for particular criteria I always use autofilter a loopless approach:

Code:
Sub AlexMach()

With Range("AD1:AD360")

    .AutoFilter Field:=1, Criteria1:="0"
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete Shift:=xlUp
    .AutoFilter
    
End With

    Columns("AD:AD").AutoFilter
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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