excel中如何自动分页小计并合计
发布网友
发布时间:2022-03-31 07:49
我来回答
共3个回答
热心网友
时间:2022-03-31 09:18
Sub Macro1()
Dim arr, lr As Long, l As Integer, subtotal, total, rng As Range
Dim i As Long, k As Integer, n As Integer, r As Long
lr = Range("A65536").End(xlUp).Row 'A列最后非空单元格行号
arr = Range("A1:a" & lr) 'A列数据区读入数组
Application.ScreenUpdating = False '关闭屏幕刷新
For i = 10 To lr '逐行查找含有"小计"和"合计"单元格
If InStr(arr(i, 1), "小计") Or InStr(arr(i, 1), "合计") Then
If rng Is Nothing Then 'IF语句把含有"小计"和"合计"单元格的行合并
Set rng = Rows(i)
Else
Set rng = Union(rng, Rows(i))
End If
End If
Next
If Not rng Is Nothing Then rng.Delete '这个行合并区域存在就整行删除
lr = Range("A65536").End(xlUp).Row 'A列数据区读入数组
arr = Range("e3:E" & lr) '把需要合计的数据区读入数组
l = WorksheetFunction.RoundUp((lr - 2) / 10, 0) '“金额”数据行数以10行为一页页数
For k = 1 To l '逐页
n = n + 1 '页面计数
subtotal = 0 '小计置零
For i = (k - 1) * 10 + 1 To k * 10 '每页逐行
If i > lr - 2 Then Exit For '如果行号大于数据总行数则退出这个for循环
subtotal = subtotal + arr(i, 1) '"小计"=本页“金额”单元格累加
Next
total = total + subtotal '"合计"=所有"小计"累加
r = n * 10 + k '插入"小计"行行号
Rows(r + 2).Insert Shift:=xlDown ''插入"小计"行,其中+2是两行表头
Cells(r + 2, 5) = subtotal '在新插入的行E列单元格写本页小计数额
Cells(r + 2, 1) = "小计"
Next
Cells(r + 3, 5) = total '最后一个小计下面写合计数额
Cells(r + 3, 1) = "合计"
Application.ScreenUpdating = True '开启屏幕刷新
End Sub
附件地址: http://club.excelhome.net/attachment.php?aid=556864&k=a0fbae713be02e397823e47e35f91d04&t=1273718773
参考资料:http://club.excelhome.net/redirect.php?fid=2&tid=96509&goto=nextoldset
热心网友
时间:2022-03-31 10:36
用透视表吧,其中有分页显示的功能,并且还可以总计,在汇总表上还可以进行表格修改后重新来一次分页显示的话所有的表格都可以以新的显示。
热心网友
时间:2022-03-31 12:11
资料-小计