Okay so here is the table set up I'm working with:
I need a macro to remove the rows containing four 0's, the only way I can think of at the moment requires the cells to be empty, ie ""
Does 0 actually count as a string or digit or is it equivalent to "" ?
I think the problem might be related to the fact that some of my 0's are text strings and others are numbers, I just didn't think this would matter.
Here, I got one for you. Try with this.
Public Sub removeRow()
Dim row As Integer
'Set the start row.
row = 1
'Loop all row from sheet until colum "A" cell is blank
Do While Sheets("sheetname").Range("A" & row) <> ""
'If all cell are 0.
If Sheets("sheetname").Range("A" & row) = 0 And Sheets("sheetname").Range("B" & row) = 0 And Sheets("sheetname").Range("C" & row) = 0 And Sheets("sheetname").Range("D" & row) = 0 Then
'Delete entire row
Sheets("sheetname").Range("A" & row).EntireRow.Delete
Else
'Increse row
row = row + 1
End If
Loop
End Sub
Try this small macro.....it examines the sum of the section of each row:
Sub RowKiller()
Dim N As Long, i As Long, wf As WorksheetFunction
Dim rng As Range
N = Cells(Rows.Count, "A").End(xlUp).Row
Set wf = Application.WorksheetFunction
For i = N To 2 Step -1
Set rng = Range(Cells(i, 1), Cells(i, 4))
If wf.Sum(rng) = 0 Then
rng.EntireRow.Delete
End If
Next i
End Sub
Create a string of rows to delete then do ONE delete. No need to poll backwards when you do it this way and it should be a LOT faster than deleting row by row:
Sub DeleteRows()
Dim i As Long, DelRange As String
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row 'Doesn't matter which way you go when you delete in one go
If CLng(Cells(i, 1)) = 0 And CLng(Cells(i, 2)) = 0 And CLng(Cells(i, 3)) = 0 And CLng(Cells(i, 4)) = 0 Then DelRange = DelRange & "," & i & ":" & i
Next i
Range(Right(DelRange, Len(DelRange) - 1)).Delete
End Sub
Used CLng to convert the string zero to a Long zero for the test.
A small word of warning though, CLng(activecell) will return 0 if the activecell is blank so blank rows will be deleted also.
Edit: Put in a IsNumeric test to counter errors when strings are encountered (Can't CLng a true string)
Sub DeleteRows()
Dim i As Long, DelRange As String
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row 'Doesn't matter which way you go when you delete in one go
If IsNumeric(Cells(i, 1)) And IsNumeric(Cells(i, 2)) And IsNumeric(Cells(i, 3)) And IsNumeric(Cells(i, 4)) Then
If CLng(Cells(i, 1)) = 0 And CLng(Cells(i, 2)) = 0 And CLng(Cells(i, 3)) = 0 And CLng(Cells(i, 4)) = 0 Then DelRange = DelRange & "," & i & ":" & i
End If
Next i
Range(Right(DelRange, Len(DelRange) - 1)).Delete
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.