Sub GetFileNames()
Dim folderPath As String
Dim fileName As String
Dim row As Integer
' フォルダ選択ダイアログを開く
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub ' キャンセルされたら終了
folderPath = .SelectedItems(1) & "\"
End With
' アクティブシートのA列にファイル名を出力
row = 1
fileName = Dir(folderPath) ' フォルダ内のすべてのファイルを対象
Do While fileName <> ""
Cells(row, 1).Value = fileName
row = row + 1
fileName = Dir() ' 次のファイル取得
Loop
MsgBox "ファイル一覧の取得が完了しました。", vbInformation
End Sub
Sub GetFileNames_fileType()
Dim folderPath As String
Dim fileName As String
Dim row As Integer
' フォルダ選択ダイアログを開く
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub ' キャンセルされたら終了
folderPath = .SelectedItems(1) & "\"
End With
' アクティブシートのA列にファイル名を出力
row = 1
fileName = Dir(folderPath & "*.jpg") ' 例: jpgファイルのみ取得
Do While fileName <> ""
Cells(row, 1).Value = fileName
row = row + 1
fileName = Dir() ' 次のファイル取得
Loop
MsgBox "ファイル一覧の取得が完了しました。", vbInformation
End Sub
コメント