データの分析、グラフ作成、および通信のためのツールを備えた Microsoft 表計算ソフトウェアのファミリ。
> はい、その通りです。
水平改ページの挿入処理に関して誤りがないのであれば、
それぞれの水平改ページが挿入されたセルを
PDF 出力範囲の基点として用いる手法が考えられます。
> 新規のブックかワークシートを生成する、という考え方ですか。
これに関しては、「 pdf だけじゃなくて xlsx でちょーだい!」
というニーズがどこかから生じる可能性が考えられたため、
あくまで一例として挙げたに過ぎません。
例えば次のようなコードであれば、水平改ページが設定された範囲ごとに
PDF ファイルを出力することが出来るはず。
(標準モジュール)
Sub ExportPDFbyHPageBreaks()
'定数の宣言
Const GroupNameColumn As String = "A" '[所属]を記録している列
'変数の宣言
Dim wbSourceBook As Excel.Workbook
Dim wsSourceSheet As Excel.Worksheet
Dim rngPrintRange As Excel.Range
Dim rngExportRange As Excel.Range
Dim strFolderPath As String
Dim strFileName As String
Dim strGroupName As String
Dim lngFirstDataRow As Long
Dim lngLastDataRow As Long
Dim lngExportStartRow As Long
Dim lngExportEndRow As Long
Dim lngExportStartColumn As Long
Dim lngExportEndColumn As Long
Dim lngBreak As Long
Dim strMsg As String
'アクティブシートを参照
Set wsSourceSheet = ActiveSheet
'そのワークブックを参照
Set wbSourceBook = wsSourceSheet.Parent
With wbSourceBook
'ブックが保存されているフォルダのサブフォルダを
'出力先フォルダとする
strFolderPath = .Path & "" & Format(Now(), "yyyymmdd")
'出力先フォルダが存在しない場合
If Dir(strFolderPath, vbDirectory) = "" Then
'出力先フォルダの作成
MkDir strFolderPath
End If
End With
With wsSourceSheet
'データ行の先頭行の番号の取得
If .PageSetup.PrintTitleRows = "" Then
'タイトル行が設定されていない場合は 1 行目とする
lngFirstDataRow = 1
Else
'タイトル行の次の行の番号を取得
lngFirstDataRow = .Range(.PageSetup.PrintTitleRows).Rows.Count + 1
End If
'印刷範囲が定義されていない場合
If .PageSetup.PrintArea = "" Then
'A1 セルから、使用中のセル範囲の最終セルまでを
'仮の印刷範囲とみなして参照
Set rngPrintRange = .Range(.Cells(1, 1), _
.UsedRange.Cells(.UsedRange.Rows.Count, _
.UsedRange.Columns.Count))
Else
'印刷範囲として定義されているセル範囲を参照
Set rngPrintRange = .Range(.PageSetup.PrintArea)
End If
'印刷範囲から情報を取得
With rngPrintRange
'印刷範囲の先頭行がデータ行の先頭行よりも下にある場合
If lngFirstDataRow < .Row Then
'印刷範囲の先頭行の番号をデータ行の先頭行の番号として設定
lngFirstDataRow = .Row
End If
'印刷範囲の最終行の番号をデータ行の終了行の番号として設定
lngLastDataRow = .Rows(.Rows.Count).Row
'印刷範囲の先頭列をそのまま PDF 出力範囲の開始列とする
lngExportStartColumn = .Column
'印刷範囲の最終列をそのまま PDF 出力範囲の終了列とする
lngExportEndColumn = .Columns(.Columns.Count).Column
End With
'水平改ページの挿入数 + 1 回ループ
For lngBreak = 0 To .HPageBreaks.Count
'PDF 出力範囲の開始行の行番号の取得
If lngBreak = 0 Then
'最初はデータ行(印刷範囲)の先頭行
lngExportStartRow = lngFirstDataRow
Else
'水平改ページが設定されているセルの行番号
lngExportStartRow = .HPageBreaks(lngBreak).Location.Row
End If
'データ行(印刷範囲)の最終行より下の行である場合
If lngExportStartRow > lngLastDataRow Then
'ループを抜ける
Exit For
End If
'PDF 出力範囲の終了行の行番号の取得
If lngBreak < .HPageBreaks.Count Then
'次の水平改ページが設定されているセルの1つ上の行の番号
lngExportEndRow = .HPageBreaks(lngBreak + 1).Location.Row - 1
'データ行(印刷範囲)の最終行より下の行である場合
If lngExportEndRow > lngLastDataRow Then
'データ行(印刷範囲)最終行に補正
lngExportEndRow = lngLastDataRow
End If
Else
'最後はデータ行(印刷範囲)の最終行
lngExportEndRow = lngLastDataRow
End If
'PDF 出力範囲の開始行の[所属]の値を取得
strGroupName = .Cells(lngExportStartRow, _
GroupNameColumn).Value
'出力先ファイルパスの生成
strFileName = strFolderPath & "" & _
Format(Date, "yyyy年mm月") & _
"_残業時間累計_" & _
Format(lngBreak + 1, "0000_") & _
strGroupName & ".pdf"
'PDF出力範囲となるセル範囲の参照
Set rngExportRange = .Range(.Cells(lngExportStartRow, lngExportStartColumn), _
.Cells(lngExportEndRow, lngExportEndColumn))
'PDF出力
rngExportRange.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strFileName, _
Quality:=xlQualityStandard
'参照解放
Set rngExportRange = Nothing
Next
End With
'参照解放
Set rngPrintRange = Nothing
Set wsSourceSheet = Nothing
Set wbSourceBook = Nothing
'メッセージの表示
strMsg = "'" & strFolderPath & "'に PDF ファイルを出力しました。" & vbCrLf & _
"フォルダを開きますか?"
If MsgBox(strMsg, vbInformation + vbYesNo, "実行完了") = vbYes Then
'[はい]ボタンがクリックされたら出力先フォルダを開く
Shell "explorer.exe """ & strFolderPath & """", vbMaximizedFocus
End If
End Sub