简体   繁体   English

如果特定工作表中的单元格等于特定值,则删除该行

[英]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.

相关问题 如果单元格值不相等,则VBA / EXCEL在特定行上拆分excel文件 - VBA/EXCEL Split excel file on specific row if cells value are not equal 如果Cell.Value是特定大小,则将该行中的3个单元格复制到新表中 - If Cell.Value is specific size, Copy 3 cells in that row to new sheet 如果值相等,则合并一特定列的单元格 - Merge Cells of one specific column if equal value 根据行数据复制特定的单元格并粘贴在特定的工作表上 - Copy specific cells according to row data and paste on specific sheet 将不同行中的特定单元格复制到单独工作表上的一行并根据原始值移动到新行 - Copy specific cells from different rows to one row on a separate sheet & move to new row based on value in original 将特定单元格从sheet2sheet复制到下一个空白行 - Copy Specific Cells from sheet2sheet to the next blank row 如何删除用户指定行中的特定单元格 - How to Delete Specific Cells in a User Specified Row 如果特定列上的单元格是唯一的,则删除该行的代码 - code to delete the row if the cells on specific column are unique 检查单元格是否包含“特定单词”然后删除行 - check if cells contain “specific word” then delete row 根据 sheet1(table1) 中同一行中的值复制特定单元格并粘贴到 sheet2(table2) 下一个可用行 - Copy specific cells based on value in the same row in sheet1(table1) and paste into sheet2(table2) next available row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM