Dim i, t, l, x, rr, dr, tt As Integer
Dim rrr As String
Public Sub Fyhz()
t = 2
Do
i = InputBox("请输入每页拟打印的行数: (不能超过一页的范围!!!)")
If i <= 0 Or i = "" Then
MsgBox ("每页行数必须大于1!")
Else
Exit Do
End If
Loop
i = Int(i)
x = i + 1
l = Range("A65536").End(xlUp).Row
Do While l >= x
Rows(x + 1).Insert Shift:=xlDown
Cells(x + 1, 1) = "本页小计"
Cells(x + 1, 2).Formula = "=SUM(R[-" + CStr(i) + "]C:R[-1]C)"
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Rows(x + 2)
x = (i + 1) * t
t = t + 1
l = l + 1
Loop
If l Mod (i + 1) <> 1 Then
rr = l Mod (i + 1)
rr = rr - 1
rrr = CStr(rr)
Cells(l + 1, 1) = "本页小计"
Cells(l + 1, 2).FormulaR1C1 = "=SUM(R[-" + rrr + "]C:R[-1]C)"
End If
Cells(l + 2, 1) = "合计"
Cells(l + 2, 2).FormulaR1C1 = "=SUM(R[-" + CStr(l + 1) + "]C:R[-1]C)/2"
Range(Cells(1, 1), Cells(l + 1, 2)).Locked = True
ActiveSheet.Protect
Cells(1, 1).Select
End Sub
Public Sub 删除分页汇总()
ActiveSheet.Unprotect
Cells.Locked = False
ActiveSheet.ResetAllPageBreaks
Rows(l + 2).Delete Shift:=xlUp
If l Mod (i + 1) = 1 Then
For dr = l To i + 1 Step -(i + 1)
Rows(dr).Delete Shift:=xlUp
Next dr
Else
Rows(l + 1).Delete Shift:=xlUp
t = t - 2
For tt = t To 1 Step -1
Rows((i + 1) * tt + 1).Delete Shift:=xlUp
Next tt
End If
Cells(1, 1).Select
End Sub