1
2

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で複数ファイルを選択するダイアログを作成

Last updated at Posted at 2020-07-05

事務作業の効率化する際に毎回検索してるので忘備のため

Sub 複数ファイルをひらく()
Dim sfile As Variant
Dim cfile As Variant
Dim path_name As String
path_name = ThisWorkbook.Path

Dim wb As Workbook
'ダイアログを開くと選択したいファイルがあるディレクトリが展開されるように記述
ChDir "C:\Users\USER\Desktop\Excel_VBA\並べ替え\バックアップフォルダ"
'ファイルダイアログを作成。複数選択可能にするためにMultiSelect:=Trueとする
sfile = Application.GetOpenFilename(filefilter:="Excelファイル,*.xlsx", MultiSelect:=True)
'IsArray関数で配列チェック。
If IsArray(sfile) = True Then

'選択したファイル毎の読込開始。今回はサンプルとして、ファイル名は必ず表示し、
'開いたファイルに[メイン]シートがあれば表示。なければファイルを閉じる処理をする

For Each cfile In sfile
Set wb = Workbooks.Open(cfile)
    MsgBox wb.Name
    Dim i As Long
    For i = 1 To wb.Sheets.Count
    If wb.Sheets(i).Name = "メイン" Then
    MsgBox wb.Sheets(i).Name
    Else
     wb.Close False
    Exit For
    End If
    Next
Next
Else
End If
End Sub
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?