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.

選択したディレクトリの配下にあるディレクトリのExcelファイルパスを全て取得

Last updated at Posted at 2024-05-24

Sub GetAllExcelFiles()
    Dim folderPath As String
    Dim filePaths As Collection
    Set filePaths = New Collection

    ' フォルダダイアログを表示してフォルダを選択
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "フォルダを選択してください"
        If .Show = -1 Then
            folderPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    ' 指定したフォルダ以下のすべてのExcelファイルを取得
    GetExcelFilesInDirectory folderPath, filePaths

    ' 取得したファイルパスを出力
    Dim filePath As Variant
    For Each filePath In filePaths
        Debug.Print filePath
    Next filePath
End Sub

Sub GetExcelFilesInDirectory(ByVal folderPath As String, ByRef filePaths As Collection)
    Dim fso As Object
    Dim folder As Object
    Dim subFolder As Object
    Dim file As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderPath)

    ' 現在のフォルダ内のファイルを処理
    For Each file In folder.Files
        If InStr(1, file.Name, ".xls", vbTextCompare) > 0 Then
            filePaths.Add file.Path
        End If
    Next file

    ' サブフォルダ内のファイルを再帰的に処理
    For Each subFolder In folder.SubFolders
        GetExcelFilesInDirectory subFolder.Path, filePaths
    Next subFolder
End Sub
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?