[英]If cells in specific sheet equal a specific value, then delete the row
I am checking all cell in a range. 我正在检查范围内的所有单元格。 If a cell's value is equal to 0 (the value is generated by a formula) then delete the row.
如果单元格的值等于0(该值由公式生成),则删除该行。
Sub CleanJunk()
For Each c In Worksheets("testsheet").Range("B33:B533").Cells
If c.Value = "0" Then Rows(c.Row).EntireRow.Delete
Next
End Sub
Right now the module runs without any error. 现在,该模块可以正常运行。 It just doesn't do anything.
它什么也没做。 No rows get deleted or affected in any way.
没有任何行被删除或以任何方式受到影响。
You haven't qualified the Rows
call, and you need to loop backwards: 您尚未限定
Rows
调用,并且需要向后循环:
Sub CleanJunk()
Dim n as long
for n = 533 to 33 step -1
If Worksheets("testsheet").Cells(n, "B").Value2 = 0 Then Worksheets("testsheet").Rows(n).EntireRow.Delete
Next
End Sub
The key here is that the value is generated by a formula. 这里的关键是值是由公式生成的。 It probably contains a floating point error.
它可能包含浮点错误。
Comparing a value to a string "0"
isn't good coding either - apples with apple not oranges! 将值与字符串
"0"
比较也不是很好的编码-苹果与苹果而不是桔子!
To get rid of the floating point error use the `ROUND(Num, # of decimals) with # of decimals = 0 要消除浮点错误,请使用`ROUND(Num,小数位数),其中小数位数= 0
Compare the result to 0. 比较结果为0。
Option Explicit 'so you don't confuse your variables
Sub CleanJunk()
Dim c as range
For Each c In Worksheets("testsheet").Range("B33:B533").Cells
'compare values rounded to integer with 0
If Round(c.Value, 0) = 0 Then c.EntireRow.Delete
Next c 'it pays to be explicit
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.