1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エクセルVBAでエクセル各シートをPDF出力する

Last updated at Posted at 2020-03-25

概要

 エクセルの各シートそれぞれをPDFに出力したいということがあった。出力ボタンを押すと、各シートがPDFに出力されるものを作りました。PDFファイル名はシート名です。

ui.png

環境

  • Excel 2016

準備

コード

vba
    'デスクトップへのパスを取得
    Dim strDesktopFullPath As String
    Dim varWSShell As Variant
    Set varWSShell = CreateObject("WScript.Shell")
    strDesktopFullPath = varWSShell.SpecialFolders("Desktop") & "\"
    Set varWSShell = Nothing

    '出力フォルダ(=デスクトップ)
    Dim strOutputFolder As String
    strOutputFolder = strDesktopFullPath
    
    'ファイル名の設定およびpdf出力(Sheet2以降)
    Dim strFileName As String
    For n = 2 To Worksheets.Count
        'セル(A2)が空のシートはpdf出力しない
        If Worksheets(n).Cells(2, 1).Value <> "" Then
            
            'ファイル名(=シート名)
            strFileName = Worksheets(n).Name
    
            'PDFに変換する
            Worksheets(n).ExportAsFixedFormat Type:=xlTypePDF, Filename:=strOutputFolder & "\" & strFileName & ".pdf"

        End If
    Next

xlsmファイルは下記GitHubへアップしてあります。
https://github.com/i-chiaki/ExcelToPdf.git

1
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?