Excel 関連情報

サンプルコード

ページヘッダのページ番号を「0詰め」して印刷する方法

  • ページヘッダのページ番号を「0詰め」して印刷する方法
    ' <使い方>
    ' 1. Excel の「Visual Basic Editor」を起動します。
    ' 2. [挿入] - [標準モジュール] を選択します。
    ' 3. 下記のコードを貼り付けます
    ' 4. [実行] - [Sub/ユーザー フォームの実行] で
    '    SpecialPrint() プロシージャを実行します。
    '    → ページヘッダのページ番号が「0詰め」されて印刷されます
    Option Explicit
    
    ' シート内のページ数を算出
    ' <参考サイト>
    ' [XL2002] 印刷されるページの総数を調べる方法
    ' http://support.microsoft.com/kb/408042/ja
    Function GetPageCount(ByRef ws As Worksheet) As Integer
        Dim H_Break As Integer
        Dim V_Break As Integer
        Dim P_Page As Integer
        Dim A_Cell As String
        A_Cell = ws.UsedRange.Address       '最後のセルのアドレスを取得
        If A_Cell = "$A$1" Then
            If IsEmpty(ws.Range(A_Cell).Value) Then
                MsgBox "印刷するデータはありません。"
                Exit Function
            End If
        End If
        H_Break = ws.HPageBreaks.Count    '横の改ページ数取得
        V_Break = ws.VPageBreaks.Count    '縦の改ページ数取得
        If V_Break = 0 Then
            P_Page = H_Break + 1
        Else
          H_Break = H_Break + 1
          V_Break = V_Break + 1
          P_Page = H_Break * V_Break
        End If
        GetPageCount = P_Page
    End Function
    
    ' 特殊印刷処理
    Sub SpecialPrint()
        Dim n As Integer
        Dim nCount As Integer
        Dim wb As Workbook
        Dim ws As Worksheet
        For Each wb In Workbooks
          For Each ws In wb.Worksheets
            ' シートのページ数を取得
            nCount = GetPageCount(ws)
            ' ページ数ぶんループ
            For n = 1 To nCount
                ' 中央ヘッダに「0詰め」した「ページ番号」を表示
                ws.PageSetup.CenterFooter = Format(n, "00")
                ' 指定ページを印刷(nページ目〜nページ目を印刷)
                ws.PrintOut n, n, 1
            Next
          Next
        Next
    End Sub

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-02-27 (土) 14:19:09 (5165d)