|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 生成每页定行打印稿3() ''页尾插入指定行/自动调整行高
Sheets("工资表").Activate
If Cells(Rows.Count, 1).End(3) = "合计" Then Exit Sub
'ActiveSheet.ResetAllPageBreaks
Dim arr, ar, a&, c&, f&, i&, j&, k&, m&, r&, ps, h#, p, y, k1
arr = [a1].CurrentRegion.Value
ar = [{6,7,8,9,10,11,13,14,15,16,18,19,20}] ''每页小计的列(列号)
r = UBound(arr): c = UBound(arr, 2)
a = 4 ''顶端标题行行数
f = 1 ''尾端合计行行数(如只插入小计1行)
ActiveSheet.PageSetup.PrintTitleRows = "$1:$" & a
k = InputBox("请输入每页行数(不包括标题行和本页小计行):", "打印页行数设定", 25) ''默认为当前页行数
m = r - a '数据行行数
p = Int(m / k)
y = m - p * k
Cells(r + 1, 1) = "本页小计"
Cells(r + 1, 1).Resize(1, 5).HorizontalAlignment = xlCenterAcrossSelection
For j = 1 To UBound(ar)
Cells(r + 1, ar(j)).FormulaR1C1 = "=SUBTOTAL(109,R[-" & y & "]C:R[-1]C)"
Next
Cells(r + 2, 1) = "合计"
Cells(r + 2, 1).Resize(1, 5).HorizontalAlignment = xlCenterAcrossSelection
For j = 1 To UBound(ar)
Cells(r + 2, ar(j)).FormulaR1C1 = "=SUBTOTAL(109,R5C:R[-1]C)"
Next
Cells(r + 1, 1).Resize(2, c).Borders.LineStyle = 1
For i = r - y + 1 To a + 2 Step -k
Rows(i).EntireRow.Insert
Cells(i, 1) = "本页小计"
Cells(i, 1).Resize(1, 5).HorizontalAlignment = xlCenterAcrossSelection
For j = 1 To UBound(ar)
Cells(i, ar(j)).FormulaR1C1 = "=SUBTOTAL(109,R[-" & k & "]C:R[-1]C)"
Next
Next
h = Rows(a + 1).RowHeight
ActiveSheet.Cells.RowHeight = h ''统一表格行高
ps = ActiveWindow.SelectedSheets.HPageBreaks(1).Location.Row - 1
k1 = ps - a - f ''每页数据行(行数)
ActiveSheet.Cells.RowHeight = Application.Round(((k1 + a + 1) * h) / (k + a + 1), 2)
End Sub
Sub 撤销每页定行打印稿3() ''恢复原数据结构
Sheets("工资表").Activate
Application.ScreenUpdating = False
Dim r&, i&
r = Cells(Rows.Count, 1).End(3).Row
For i = r To 5 Step -1
If Cells(i, 1) = "本页小计" Or Cells(i, 1) = "合计" Then
Rows(i).EntireRow.Delete
End If
Next
ActiveSheet.ResetAllPageBreaks
Application.ScreenUpdating = True
End Sub
|
|