自动在最后一行添加合计,听说没几个人会用!





'作者:Excel办公实战-小易'功能:自动写入合计'------------------------------------------------------------------Private Sub Worksheet_Change(ByVal Target As Range)
Dim maxRow As Long '避免事件反复触发! Application.EnableEvents = False '小于第三列和大于第2行触发事件(根据实际情况修改) If Target.Column < 3 And Target.Row > 1 Then '获取有内容的最大行(最后一行) maxRow = Cells(Rows.Count, 1).End(3).Row If Cells(maxRow, 1) <> "合计" Then '没有合计,写入合计 Cells(maxRow + 1, 1) = "合计" Cells(maxRow + 1, 2) = _ Application.Sum(Range("B2").Resize(maxRow, 1)) Else '合计存在直接在原位置更新 Cells(maxRow, 2) = _ Application.Sum(Range("B2").Resize(maxRow - 2, 1)) End If '设置格式 With Range("A1").CurrentRegion .Borders.LineStyle = 1 .Font.Bold = False End With '重新获取有内容的最大行 maxRow = Cells(Rows.Count, 1).End(3).Row '设置字体加粗 Cells(maxRow, 1).Resize(1, 2).Font.Bold = True End If Application.EnableEvents = TrueEnd Sub

小结:思路决定出路,很多看似“难题”,其实只是看问题的角度问题而已!

赞 (0)
