首页 > 解决方案 > 如果不包括最后一行的列总和 = 另一个单元格中的值

问题描述

如果整个列(不包括最后一行,因为这是总数)中的值的总和等于我们没有在最后一行中的单元格,则尝试创建一个子项,该子项将删除财务报表中的总行包括(总金额)

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

标签: excelvba

解决方案


假设您的总数在最后一行,您可以跳过最后一行:

这样做: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 行(使用上面的测试代码):

在此处输入图像描述


推荐阅读