- 帖子
- 522
- 主題
- 36
- 精華
- 1
- 積分
- 603
- 點名
- 0
- 作業系統
- win xp sp3
- 軟體版本
- Office 2003
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2012-12-13
- 最後登錄
- 2021-7-11
|
本帖最後由 yen956 於 2014-4-8 13:32 編輯
回復 5# gn00713242
花了一整夜的測試與修改, 終於完成了,
請試試看:- '修改自下列網頁:
- 'http://www.ozgrid.com/forum/showthread.php?t=83326
- 'Border Around Each Printed Page
- 'VB:
- Sub testPages()
- Dim lastRow, lastCol, lastRow2, lastCol2 As Integer
- Dim HPBreak, VPBreak, totHP, totVP As Integer
- Dim cVP, rHP, row1, cow1, flagHP, flagVP As Integer
- Dim ar
-
- With ActiveSheet
-
- '取得列印範圍 的 下邊界及右邊界
- '這是比較較笨的方法, 如有較簡捷的方法, 煩請告知
- strPrintArea = .PageSetup.PrintArea
- ar = Split(strPrintArea, ":")
- ar = Split(ar(1), "$")
- lastRow = --ar(2)
- lastCol = Asc(ar(1)) - 64
-
- totVP = .VPageBreaks.Count '取得 垂直分頁線 的 總數
- totHP = .HPageBreaks.Count '取得 水平分頁線 的 總數
-
- '取得 最大水平分頁線 所在的 下邊界
- lastRow2 = .HPageBreaks(totHP).Location.Row - 1
- '取得 最大垂直分頁線 所在的 右邊界
- lastCol2 = .VPageBreaks(totVP).Location.Column - 1
-
- '公式:列印總頁數 = (VPageBreaks.Count + 1)*(HPageBreaks.Count+1)
- '並非一體適用, why? 請參考下圖即知:
- '以下修正 列印總頁數 的公式
- If lastRow2 = lastRow Then
- flagHP = 0
- Else
- flagHP = 1
- End If
- If lastCol2 = lastCol Then
- flagVP = 0
- Else
- flagVP = 1
- End If
-
- totHP = totHP + flagHP
- totVP = totVP + flagVP
-
- '計算全部列印頁數
- totPg = (totVP) * (totHP)
-
- '也可透過 Excel4Macro 取得全部列印頁數 (Excel 2003以下適用)
- 'totPg = ExecuteExcel4Macro("Get.Document(50)")
-
- cow1 = 1
- currentP = 1
-
- For VPBreak = 1 To totVP
-
- If VPBreak = totVP Then
- cVP = lastCol
- Else
- cVP = .VPageBreaks(VPBreak).Location.Column - 1
- End If
-
- For HPBreak = 1 To totHP
- '取得目前水平分頁線的下邊界
-
- If HPBreak = totHP Then
- rHP = lastRow
- Else
- rHP = .HPageBreaks(HPBreak).Location.Row - 1
- End If
- .Cells(rHP, Int((cVP - cow1 + 1) / 2) + cow1) = "共" & totPg & "頁之第" & currentP & "頁"
- currentP = currentP + 1
- Next
- cow1 = cVP + 1
- Next
- End With
-
- End Sub
複製代碼 |
|