0
1

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.

ExcelVBAでダイアログを開いてファイル名を取得し、セルへ入力する

Last updated at Posted at 2020-11-27

image.png

これはExcelの名簿から一括でメール配信やFAX送付状を作るVBAの各コードを細かく解説したものです。
完成品はこちらのGitHubからダウンロードできます。

ダイアログからパス名を取得

「ファイル名を取得」ボタンをクリックしたらダイアログが開き、選択したファイルのパスをセルに入力するコードの解説です。

コード解説

Sub ファイル名を入力(cellAddress)
    
    'ファイルパスを宣言
    Dim filePath As String

    'ファイルパスをセルアドレスに入力
    filePath = Application.GetOpenFilename()
    Range(cellAddress).Value = filePath
    
End Sub

Sub B6へ入力()
    ファイル名を入力 "B6"
End Sub

Sub B7へ入力()
    ファイル名を入力 "B7"
End Sub

Sub B8へ入力()
    ファイル名を入力 "B8"
End Sub

上から順に説明していくと
・何度か使用するためSub ファイル名を入力(引数)というように引数を設定する
・filePathを文字列として宣言
・filePathはダイアログを開いて選択したもの
・取得したファイルパスを引数の場所に入力

・他のSubはそのままの名前で呼び出せるので
B6へ入力したいときは
ファイル名を入力 "B6"
というように「Subの名前スペース引数」を指定すると使用することができる

ボタンに割り当てる

今回は図形ではなくフォームに割り当てた。
開発→挿入→フォームでボタンを選択し、配置したフォームを右クリックでマクロを登録する。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?