[英]Macro in Excel to remove rows containing all 0's
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 "" 我需要一个宏来删除包含四个0的行,此刻我能想到的唯一方法是将单元格为空,即“”
Does 0 actually count as a string or digit or is it equivalent to "" ? 0实际算作字符串还是数字,或者等于“”?
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. 我认为问题可能与以下事实有关:我的一些0是文本字符串,而另一些是数字,我只是认为这并不重要。
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. 使用CLng将字符串零转换为测试的Long零。
A small word of warning though, CLng(activecell) will return 0 if the activecell is blank so blank rows will be deleted also. 提醒一下,如果活动单元为空白,则CLng(activecell)将返回0,因此空白行也将被删除。
Edit: Put in a IsNumeric test to counter errors when strings are encountered (Can't CLng a true string) 编辑:遇到字符串时,输入IsNumeric测试以应对错误(无法捕获真实字符串)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.