excel - 如果不包括最后一行的列总和 = 另一个单元格中的值
问题描述
如果整个列(不包括最后一行,因为这是总数)中的值的总和等于我们没有在最后一行中的单元格,则尝试创建一个子项,该子项将删除财务报表中的总行包括(总金额)
Dim number As Double
Dim RowDelete As Long
RowDelete = Cells(Rows.Count, 36).End(xlUp).Row
number = Application.WorksheetFunction.Sum(Range(Cells(1, 1), Cells(RowDelete - 1, 1)))
If number = RowDelete Then
Cells(Rows.Count, 1).End(xlUp).Delete
End If
解决方案
假设您的总数在最后一行,您可以跳过最后一行:
这样做:lRow = Cells(Rows.Count, 1).End(xlUp).Row
- 检查哪一行是最后一行
Cells(1, 1)
-> 单元格(第 nr 行,第 nr 列)
代码:
Sub ifSum()
Dim number As Double
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row
number = Application.WorksheetFunction.Sum(Range(Cells(1, 1), Cells(lRow - 1, 1)))
If number = Range("C1") Then
Range("D4").FormulaR1C1 = "Hello!"
Else
Range("D4").FormulaR1C1 = "Goodbye!"
End If
End Sub
这个“测试代码”似乎对我有用:
Sub ifSum()
Dim number As Double
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row
number = Application.WorksheetFunction.Sum(Range(Cells(1, 1), Cells(lRow - 1, 1)))
If number = lRow Then
Cells(Rows.Count, 1).End(xlUp).Delete
End If
End Sub
鉴于我的工作表看起来像这样,在这种情况下它将删除第 15 行(使用上面的测试代码):
推荐阅读
- excel - 使用 Excel VBA Dir() 函数提前退出循环并打开文件目录
- css - 容器类问题中的 Bootstrap 5 侧边栏和导航栏
- python - CircuitPython 类期望 __init__ 上的 args 数据类型错误
- html - 如何在同一代码库中管理移动应用程序的内部和外部模式
- r - 如何根据其他列R中的值对一列中的值求和?
- javascript - 打字稿反应esbuild无法构建
- raspberry-pi - 无法下载 sshfs 库
- c# - 将实体模型序列化为 JSON 时检测到可能的对象循环
- java - Java - Elasticsearch 自定义插件使用 nodeClient 发送带有正文的请求
- python - 使用嵌套字典映射数据框中的值