parkerbelt
Active Member
- Joined
- May 23, 2014
- Messages
- 377
I'm looking for the vba code to delete a row if a column contains a zero
Sub delete_Me()
Dim MyColumn As String, LastRow As Long
Dim x As Long
MyColumn = "B"
LastRow = Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row
For x = LastRow To 1 Step -1
If Len(Cells(x, MyColumn)) = 1 And Cells(x, MyColumn).Value = 0 Then
Rows(x).Delete
End If
Next
End Sub
Hi,
Try this, change the column letter to the correct one.
Code:Sub delete_Me() Dim MyColumn As String, LastRow As Long Dim x As Long MyColumn = "B" LastRow = Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row For x = LastRow To 1 Step -1 If Len(Cells(x, MyColumn)) = 1 And Cells(x, MyColumn).Value = 0 Then Rows(x).Delete End If Next End Sub
Sub DeleteRowsWithZeroInColumn()
Columns("B").Replace 0, "#N/A", xlWhole
On Error Resume Next
Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
Does this macro work for you...
Code:Sub DeleteRowsWithZeroInColumn() Columns("B").Replace 0, "#N/A", xlWhole On Error Resume Next Columns("B").SpecialCells(xlConstants, xlErrors).EntireRow.Delete On Error GoTo 0 End Sub
Sub FastRemove()
Dim rngData As Range
Dim rngWithoutHeader As Range
Dim rngDelete As Range
Set rngData = Range("A1:E7")
With rngData
Set rngWithoutHeader = .Rows("2:" & .Rows.Count)
End With
rngData.AutoFilter Field:=1, Criteria1:=0 'Filter by zero
On Error Resume Next 'Error handling is necessary in case if nothing is filtered
Set rngDelete = rngWithoutHeader.SpecialCells(xlCellTypeVisible)
If Err = 0 Then
rngDelete.EntireRow.Delete
End If
rngData.AutoFilter 'Remove autofilter
End Sub
Sub delete_Me()
Dim MyColumn As String, LastRow As Long
Dim x As Long
MyColumn = "B"
LastRow = Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row
On Error Resume Next
For x = LastRow To 1 Step -1
If Not (IsError(Cells(x, MyColumn))) Then
If Len(Cells(x, MyColumn)) = 1 And Cells(x, MyColumn).Value = 0 Then
Rows(x).Delete
End If
End If
Next
On Error GoTo 0
End Sub
Then I don't think the cells you say have a 0 in them contain only a 0 all by itself in them... there may be a non-visible character in with them as well (a space or a non-breaking space, ASCII 160). Let's say B5 contains what you think is a 0 (and also is a cell that my code did not change), put this formula in an empty cell and see if the value is more than 1...That didn't seem to work either.
I stepped through the code and it just went straight through to End Sub and never looped.
Using AutoFilter is the fastest method I know of. Here's example of how to use it:
Code:Sub FastRemove() Dim rngData As Range Dim rngWithoutHeader As Range Dim rngDelete As Range Set rngData = Range("A1:E7") With rngData Set rngWithoutHeader = .Rows("2:" & .Rows.Count) End With rngData.AutoFilter Field:=1, Criteria1:=0 'Filter by zero On Error Resume Next 'Error handling is necessary in case if nothing is filtered Set rngDelete = rngWithoutHeader.SpecialCells(xlCellTypeVisible) If Err = 0 Then rngDelete.EntireRow.Delete End If rngData.AutoFilter 'Remove autofilter End Sub
Hi,
That will be because you have errors in the range.
Code:Sub delete_Me() Dim MyColumn As String, LastRow As Long Dim x As Long MyColumn = "B" LastRow = Cells(Cells.Rows.Count, MyColumn).End(xlUp).Row On Error Resume Next For x = LastRow To 1 Step -1 If Not (IsError(Cells(x, MyColumn))) Then If Len(Cells(x, MyColumn)) = 1 And Cells(x, MyColumn).Value = 0 Then Rows(x).Delete End If End If Next On Error GoTo 0 End Sub