0
0

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 1 year has passed since last update.

PDFを作成する一番簡単なマクロの例

Last updated at Posted at 2024-04-13

0.VBAエディタを開く。

通常はAlt + F11を押す。

1.標準モジュールを作成

image.png

2.標準モジュールとクラスモジュール

標準モジュールはグローバルな手続きや変数を定義するために使用され、クラスモジュールはオブジェクト指向プログラミングの原則に従ってデータと操作をカプセル化するために使用される。

3.PDFを作成するコードを作成

Sub CreatePDF()
    Dim ws As Worksheet
    Dim rng As Range
    Dim fileSaveName As Variant
    
    ' 保存するシートを指定
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' 保存する範囲を指定
    Set rng = ws.Range("A1:D10")
    
    ' ファイル名を指定して保存ダイアログを表示
    fileSaveName = Application.GetSaveAsFilename(fileFilter:="PDF Files (*.pdf), *.pdf", Title:="Save PDF As")
    
    ' ユーザーがキャンセルボタンを押した場合、処理を終了
    If fileSaveName = False Then Exit Sub
    
    ' PDFに出力
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fileSaveName, Quality:=xlQualityStandard
End Sub

4.「挿入」からボタンコントロールを選択してマクロを登録

image.png

image.png

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?