excel中如何自动分页小计并合计
3个回答
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询