【VBA】空白セルを部分一致でPDF保存する方法
手作業で大量のデータから特定のパターンを抽出し、PDFに保存するのは時間がかかります。この記事では、空白セルが含まれる行だけを選択してPDFに出力するVBAマクロを作成します。
サンプルコード
VBA
Option Explicit
Sub SavePartialMatchRowsAsPDF()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
Application.ScreenUpdating = False
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 1 To lastRow
If InStr(1, ws.Cells(i, 1), "") > 0 Then '部分一致検索
ws.Rows(i).Select
End If
Next i
With ws.PageSetup
.PrintArea = Application.Address
.Orientation = xlPortrait
.PaperSize = xlA4Paper
.FitToPagesWide = 1
.FitToPagesTall = Sheet1.UsedRange.Rows.Count / 50 + IIf(Sheet1.UsedRange.Rows.Count Mod 50 > 0, 1, 0)
End With
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\Public\Documents\PartialMatchRows.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Application.ScreenUpdating = True
End Subよくある質問
Q 元に戻せますか?
A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。
Q エラーが出たら?
A.
シート名や列番号が正しいか確認してください。