
今日は指定したフォルダーにあるファイル名リストを一括作成する方法を紹介します。



更新日時とサイズ情報付きだね!
- 任意のフォルダーを指定
- ワークシート Sheet1へリスト出力




目次
フォルダー内のファイル名リスト一括作成 サンプル
フォルダー内のファイル情報をリストとして一括出力したい
このようなケースで、指定したフォルダーからWorksheet “Sheet1″にリストを一括出力するサンプルになります。
マクロが存在するファイルのWorksheet”Sheet1″にデータが抽出されるので注意してください。
Worksheet”Sheet1″にヘッダー情報を下記のように準備してください。


'フォルダー内のファイル名リスト一括作成
Sub FilesInFolder()
Application.ScreenUpdating = False
Dim folder_Path As String
Dim FileName As String
Dim i As Long
Call ClearFilesInFolder
ThisWorkbook.Worksheets("Sheet1").Activate
MsgBox " フォルダーを選択してね( ー`дー´)キリッ"
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = " フォルダーを選択"
If .Show = True Then
folder_Path = .SelectedItems(1)
Else
MsgBox "終了します(TдT)"
Exit Sub
End If
End With
FileName = Dir(folder_Path & "\*.*")
i = 2
Do Until FileName = ""
Cells(i, 1) = FileName
Cells(i, 2) = FileDateTime(folder_Path & "\" & FileName)
Cells(i, 3) = Format(FileLen(folder_Path & "\" & FileName), "#,##0")
FileName = Dir()
i = i + 1
Loop
End Sub
Sub ClearFilesInFolder()
ThisWorkbook.Worksheets("Sheet1").Activate
Range("A2:C1048576").Clear
End Sub
まとめ
このサンプルを活用いただけると、フォルダーにある大量のファイル情報も瞬時にリスト化することができます。
この記事がミス撲滅や生産性向上につながるととても嬉しいです。
VBA初級から抜け出すための知恵と、実務で活用できるスキルが学べる1冊です!激しくオススメ!


コメント