excel - Delete Row if populated with blanks and zeros within a range
问题描述
I have a report where i need to remove the rows with no data in the cells or a zero within a range column C to O.
This code i have almost does it perfectly, but i found a fatal flaw in the logic. If the row has a positive and negative value that sum to zero it will be deleted while i would still need to retain that row.
I really appreciate the assistance from this site as I have been able to really automate many of my reports and help people in other departments! you guys rock! Thank You!
Dim rw As Long, i As Long
rw = Cells(Rows.Count, 1).End(xlUp).Row
For i = rw To 6 Step -1
If Application.Sum(Cells(i, 3).Resize(1, 17)) = 0 Then
Rows(i).Delete
End If
Next
解决方案
不是检查 SUM,而是遍历每个单元格并检查其是否有效。
为了更好地解释这一点,我将为您使用伪代码:
- 创建一个标志变量并将其设置为 false
- 创建一个循环检查一行中的每个单元格
- 如果找到有效数字,则将标志设置为 true
- 在移动到下一个单元格之前,检查您的标志是否仍然为假
- 如果您的标志为假-> 继续下一个单元格
- 循环到行中所有单元格的末尾
伪代码变成粗代码
Dim rw As Long, i As Long
Dim rng As Range
Dim validRow As Boolean
validRow = false
rw = Cells(Rows.Count, 1).End(xlUp).Row
For i = rw To 6 Step -1
Set rng = (Cells(i, 3).Resize(1, 17))
For Each cell In rng
If Not IsEmpty(cell) Then
If cell.value <> 0 Then
validRow = true
End If
End If
If validRow = true Then
Exit For
End If
Next cell
If validRow = false Then
Rows(i).Delete
End If
validRow = false
Next
[@LL 编辑:将 >0 更改为 <>0 也寻找任何不同于零的内容,以说明仅填充负值的行]
推荐阅读
- javascript - 如何根据js请求的响应在页面中生成动态行?(Django 模板/HTML)
- python - 使用 if else 创建数学测验
- java - Android 导航组件和闪屏 API
- azure - 不支持 Microsoft /oauth2/v2.0/authorize 'amr_values' 请求参数。(外交部)
- python - 如何在 PySimpleGUI Table 元素的单元格内使用按钮?
- html - CSS 固定位置继承宽度不等于父级
- python - 如何以更简洁的方式为 discord.py 编写嵌入代码?
- c# - 部署的 gRPC 应用程序将仅使用端口 5000 - 需要它来监听端口 https://asdf.com:30000
- python - ImportError:无法从 open3d 导入名称“linux”
- r - 使用 rstan 运行回归估计