|
imlzrlzr 发表于 2012-8-5 00:48
谢谢了!明白了!
参考一下里面的代码。
Sub 插入每页小计累计()
Dim x$, r, a, b, k, m, n, i, j
If MsgBox("你确定要插入本页合计&本页累计吗?", vbYesNo, "★ ★ ★ ★ 王德荣提示 ★ ★ ★ ★") = vbYesNo Then Exit Sub
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
a = 3 '顶端标题行行数
k = ActiveWindow.SelectedSheets.HPageBreaks(1).Location.Row - a - 3 '[减3:小计行+累计行+1(常数)]
r = [a65536].End(3).Row
b = (r - a) Mod k
If b > 0 Then n = k - (r - a) Mod k Else n = 0
For i = r + n To a + k - 1 Step -k
Cells(i, 1).Offset(1, 0).Resize(2, 1).EntireRow.Insert
Cells(i + 1, 1) = "本页合计": Cells(i + 2, 1) = "本页累计"
Cells(i + 1, 1).Resize(1, 33).Interior.ColorIndex = 36
Cells(i + 2, 1).Resize(1, 33).Interior.ColorIndex = 34
For j = 2 To 33
Cells(i + 2, j) = WorksheetFunction.Sum(Range(Cells(3, j), Cells(i, j)))
Cells(i + 1, j) = WorksheetFunction.Sum(Range(Cells(i - k + 1, j), Cells(i, j)))
Next
Next
r = [a65536].End(3).Row
Cells(r - n - 1, 1).Resize(n + 2, 33).Borders.LineStyle = 1
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub |
|