返回列表 上一主題 發帖

[發問] 請問各位先進,如何用VBA寫在儲存格裡該頁頁碼?

回復 1# gn00713242
試試看(VBA):
   ActiveSheet.Index & "/" & Worksheets.Count & "頁"

TOP

本帖最後由 yen956 於 2014-4-1 13:41 編輯

回復 3# gn00713242
找到了,
http://gb.twbts.com/index.php?topic=4194.msg21593
請參考

TOP

本帖最後由 yen956 於 2014-4-8 13:32 編輯

回復 5# gn00713242
花了一整夜的測試與修改, 終於完成了,
請試試看:
  1. '修改自下列網頁:
  2. 'http://www.ozgrid.com/forum/showthread.php?t=83326
  3. 'Border Around Each Printed Page
  4. 'VB:

  5. Sub testPages()
  6.     Dim lastRow, lastCol, lastRow2, lastCol2 As Integer
  7.     Dim HPBreak, VPBreak, totHP, totVP As Integer
  8.     Dim cVP, rHP, row1, cow1, flagHP, flagVP As Integer
  9.     Dim ar
  10.    
  11.     With ActiveSheet
  12.    
  13.         '取得列印範圍 的 下邊界及右邊界
  14.         '這是比較較笨的方法, 如有較簡捷的方法, 煩請告知
  15.         strPrintArea = .PageSetup.PrintArea
  16.         ar = Split(strPrintArea, ":")
  17.         ar = Split(ar(1), "$")
  18.         lastRow = --ar(2)
  19.         lastCol = Asc(ar(1)) - 64
  20.         
  21.         totVP = .VPageBreaks.Count    '取得 垂直分頁線 的 總數
  22.         totHP = .HPageBreaks.Count    '取得 水平分頁線 的 總數
  23.         
  24.         '取得 最大水平分頁線 所在的 下邊界
  25.         lastRow2 = .HPageBreaks(totHP).Location.Row - 1
  26.         '取得 最大垂直分頁線 所在的 右邊界
  27.         lastCol2 = .VPageBreaks(totVP).Location.Column - 1
  28.         
  29.         '公式:列印總頁數 = (VPageBreaks.Count + 1)*(HPageBreaks.Count+1)
  30.         '並非一體適用, why? 請參考下圖即知:
  31.         '以下修正 列印總頁數 的公式
  32.         If lastRow2 = lastRow Then
  33.             flagHP = 0
  34.         Else
  35.             flagHP = 1
  36.         End If
  37.         If lastCol2 = lastCol Then
  38.             flagVP = 0
  39.         Else
  40.             flagVP = 1
  41.         End If
  42.         
  43.         totHP = totHP + flagHP
  44.         totVP = totVP + flagVP
  45.         
  46.         '計算全部列印頁數
  47.         totPg = (totVP) * (totHP)
  48.         
  49.         '也可透過 Excel4Macro 取得全部列印頁數 (Excel 2003以下適用)
  50.         'totPg = ExecuteExcel4Macro("Get.Document(50)")
  51.         
  52.         cow1 = 1
  53.         currentP = 1
  54.       
  55.         For VPBreak = 1 To totVP
  56.             
  57.             If VPBreak = totVP Then
  58.                 cVP = lastCol
  59.             Else
  60.                 cVP = .VPageBreaks(VPBreak).Location.Column - 1
  61.             End If
  62.             
  63.             For HPBreak = 1 To totHP
  64.                 '取得目前水平分頁線的下邊界
  65.                
  66.                 If HPBreak = totHP Then
  67.                     rHP = lastRow
  68.                 Else
  69.                     rHP = .HPageBreaks(HPBreak).Location.Row - 1
  70.                 End If
  71.                 .Cells(rHP, Int((cVP - cow1 + 1) / 2) + cow1) = "共" & totPg & "頁之第" & currentP & "頁"
  72.                 currentP = currentP + 1
  73.             Next
  74.             cow1 = cVP + 1
  75.         Next
  76.     End With
  77.      
  78. End Sub
複製代碼

TOP

回復 8# gn00713242
我也在學, 邊學邊賣, 謝啦!!

TOP

        靜思自在 : 多做多得。少做多失。
返回列表 上一主題