返回列表 上一主題 發帖

[發問] 表單列印超過一頁範圍,可否在列印時單頁加總數值?

回復 6# zhiling
因為每台印表機設定一頁有幾個 Rows 是不一定
這裡試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Msg As String, xi As Integer
  4.     With ActiveSheet
  5.         If Application.CountA(.Range("a1").CurrentRegion) = 0 Then
  6.             MsgBox "範圍內沒有資料可印列"
  7.             Exit Sub
  8.         End If
  9.         ActiveWindow.View = xlPageBreakPreview
  10.         .PageSetup.PrintArea = .Range("a1").CurrentRegion.Address
  11.         .PageSetup.PrintTitleRows = "$1:$1"
  12.         .PageSetup.LeftFooter = "&14&N 頁-  &P"
  13.         '頁尾右邊:   &14-> 字體Size =14, &N->總頁數,  &P->第幾頁
  14.         For xi = 0 To .HPageBreaks.Count
  15.             Msg = "&14 小計: "                   '&14:  字體Size =14
  16.             If xi = 0 Then
  17.                 Msg = Msg & Application.Sum(.Range([b2], .HPageBreaks(1).Location.Cells(0, 2)))
  18.             ElseIf xi > 0 And xi < .HPageBreaks.Count Then
  19.                 Msg = Msg & Application.Sum(.Range(.HPageBreaks(xi).Location.Cells(1, 2), .HPageBreaks(xi + 1).Location.Cells(0, 2)))
  20.                 Msg = Msg & Chr(10) & "累計: " & Application.Sum(.Range([b2], .HPageBreaks(xi + 1).Location.Cells(0, 2)))
  21.             Else
  22.                 Msg = Msg & Application.Sum(.Range(.HPageBreaks(.HPageBreaks.Count).Location, .[b2].End(xlDown)))
  23.                 Msg = Msg & Chr(10) & "累計: " & Application.Sum(.Range("B:B"))
  24.             End If
  25.         .PageSetup.RightFooter = Msg                        '頁尾左邊:
  26.          .PrintOut From:=xi + 1, To:=xi + 1, Copies:=1      '一頁一頁印列
  27.         Next
  28.         .DisplayPageBreaks = False                          '取消分頁線
  29.     End With
  30.     ActiveWindow.View = xlNormalView
  31.     MsgBox "印列 完成"
  32. End Sub
複製代碼
應用在 3# 檔案 如圖的資料中

TOP

        靜思自在 : 有多少力量就做多少事,不要心存等待,等待才會落空。
返回列表 上一主題