|
Sub 新建分页小计()
Dim iSubCol As Integer, rSubArea As Range
Dim hb As HPageBreak
Worksheets("sheet1").Activate '激活工作表“sheet1”
Rows(Range("a65536").End(xlUp).Row).Copy Range("a" & Range("a65536").End(xlUp).Row + 1)
Rows(Range("a65536").End(xlUp).Row).ClearContents
Range("a" & Range("a65536").End(xlUp).Row + 1) = " "
ActiveWindow.View = xlPageBreakPreview ' 进入 分页浏览 模式, 以便 EXCEL 正确计页
Set r1stSubCell = Range("A2") ' 本例名单从 A5 单元格开始
iSubCol = 9 ' 本例小计项共有 21 列
' 最后一行插入手工分页符
' ActiveSheet.HPageBreaks.Add Before:=r1stSubCell.End(xlDown).Offset(1, 0)
' ActiveSheet.HPageBreaks.Add Before:=Range("a65536").End(xlUp)
' 测试每一个分页符,
' 如果是自动分页符, 则在其上一行插入一小计行, 而本行纳入下一页
' 否则, 在本行插入一小计行
For Each hb In ActiveSheet.HPageBreaks
Set rCurrentCell = hb.Location
rCurrentCell.Select ' 看看先
If hb.Type = xlPageBreakAutomatic Then Set rCurrentCell = rCurrentCell.Offset(-2, 0)
rCurrentCell.EntireRow.Insert
rCurrentCell.EntireRow.Insert
Set rCurrentCell = rCurrentCell.Offset(-2, 0)
' 添加分页小计内容
With rCurrentCell
.Value = "本页小计"
.Font.Bold = True
'Set rSubArea = Application.Union(Range("e" & rCurrentCell.Row), Range("g" & rCurrentCell.Row)) ' 需要填充分页小计公式的区域V列
'Set rSubArea = Range("e" & rCurrentCell.Row, "g" & rCurrentCell.Row) ' 需要填充分页小计公式的区域V列''B至V列的区域(共21列)
r = rCurrentCell.Row
Set rSubArea = Union(Range("e" & r), Range("f" & r), Range("g" & r)) ''填充分页小计公式的区域仅是其中3列(f、i、j)
' 使用 SUBTOTAL 公式的好处是方便扩展, 且不会对已计算区域重复计算(如果可能发生这种情况的话)
rSubArea.Formula = "=SUBTOTAL(9," & r1stSubCell.Offset(0, 4).Address(1, 0) & ":" & .Offset(-1, 4).Address(1, 0) & ")"
Set rCurrentCell = rCurrentCell.Offset(1, 0)
End With
With rCurrentCell
.Value = "本页累计"
.Font.Bold = True
'Set rSubArea = Application.Union(Range("e" & rCurrentCell.Row), Range("g" & rCurrentCell.Row)) ' 需要填充分页小计公式的区域V列
'Set rSubArea = Range("e" & rCurrentCell.Row, "g" & rCurrentCell.Row) ' 需要填充分页小计公式的区域V列''B至V列的区域(共21列)
r = rCurrentCell.Row
Set rSubArea = Union(Range("e" & r), Range("f" & r), Range("g" & r)) ''填充分页小计公式的区域仅是其中3列(f、i、j)
' 使用 SUBTOTAL 公式的好处是方便扩展, 且不会对已计算区域重复计算(如果可能发生这种情况的话)
rSubArea.Formula = "=SUBTOTAL(9," & "e2:" & .Offset(-1, 4).Address(1, 0) & ")"
Set r1stSubCell = .Offset(1, 0)
End With
Next
Rows(Range("a65536").End(xlUp).Row).Clear
If Range("A65536").End(xlUp) = " " Then Rows(Range("a65536").End(xlUp).Row).Clear
Rows(Range("a65536").End(xlUp).Row).Copy Range("a" & Range("a65536").End(xlUp).Row + 1)
Rows(Range("a65536").End(xlUp).Row).Copy Range("a" & Range("a65536").End(xlUp).Row + 1)
Rows(Range("a65536").End(xlUp).Row).Copy Range("a" & Range("a65536").End(xlUp).Row + 1)
Rows(Range("a65536").End(xlUp).Row).ClearContents
Rows(Range("a65536").End(xlUp).Row).ClearContents
Rows(Range("a65536").End(xlUp).Row).ClearContents
Set rCurrentCell = Range("a65536").End(xlUp).Offset(1, 0)
With rCurrentCell
.Value = "本页小计"
.Font.Bold = True
'Set rSubArea = Application.Union(Range("e" & rCurrentCell.Row), Range("g" & rCurrentCell.Row)) ' 需要填充分页小计公式的区域V列
'Set rSubArea = Range("e" & rCurrentCell.Row, "g" & rCurrentCell.Row) ' 需要填充分页小计公式的区域V列''B至V列的区域(共21列)
r = rCurrentCell.Row
Set rSubArea = Union(Range("e" & r), Range("f" & r), Range("g" & r)) ''填充分页小计公式的区域仅是其中3列(f、i、j)
' 使用 SUBTOTAL 公式的好处是方便扩展, 且不会对已计算区域重复计算(如果可能发生这种情况的话)
rSubArea.Formula = "=SUBTOTAL(9," & r1stSubCell.Offset(0, 4).Address(1, 0) & ":" & .Offset(-1, 4).Address(1, 0) & ")"
Set rCurrentCell = rCurrentCell.Offset(1, 0)
End With
With rCurrentCell
.Value = "本页累计"
.Font.Bold = True
'Set rSubArea = Application.Union(Range("e" & rCurrentCell.Row), Range("g" & rCurrentCell.Row)) ' 需要填充分页小计公式的区域V列
'Set rSubArea = Range("e" & rCurrentCell.Row, "g" & rCurrentCell.Row) ' 需要填充分页小计公式的区域V列''B至V列的区域(共21列)
r = rCurrentCell.Row
Set rSubArea = Union(Range("e" & r), Range("f" & r), Range("g" & r)) ''填充分页小计公式的区域仅是其中3列(f、i、j)
' 使用 SUBTOTAL 公式的好处是方便扩展, 且不会对已计算区域重复计算(如果可能发生这种情况的话)
rSubArea.Formula = "=SUBTOTAL(9," & "e2:" & .Offset(-1, 4).Address(1, 0) & ")"
Set r1stSubCell = .Offset(1, 0)
End With
With Range("A" & Range("A65536").End(xlUp).Row + 1)
.Value = "总 计"
.Font.Bold = True
End With
'Set rSubArea = Range("e" & Range("A65536").End(xlUp).Row, "g" & Range("A65536").End(xlUp).Row) 'V列
r = Range("A65536").End(xlUp).Row
Set rSubArea = Union(Range("e" & r), Range("f" & r), Range("g" & r)) ''其中3列(f\i\j)
rSubArea.Offset(0, 0) = "=SUBTOTAL(9," & "e2:e" & Range("A65536").End(xlUp).Row - 1 & ")"
删除分页符
ActiveWindow.View = xlNormalView
End Sub |
|