Excel VBA 逆引き集 | フォルダ一覧を取得

Excel VBA
スポンサーリンク

フォルダ一覧を取得

「指定フォルダの中にあるサブフォルダを一覧化したい」場面は、ログ管理やデータ整理でよくあります。初心者でもすぐ使える Dir関数FileSystemObject (FSO) の2つの方法を中心に、例題付きで解説します。


方法①:Dir関数でサブフォルダ一覧を取得(最短)

Sub ListFolders_Dir()
    Dim path As String, folder As String, r As Long
    path = "C:\Data\"          '対象フォルダ
    folder = Dir(path, vbDirectory)
    r = 2

    Do While folder <> ""
        '「.」「..」やファイルを除外
        If (GetAttr(path & folder) And vbDirectory) = vbDirectory Then
            If folder <> "." And folder <> ".." Then
                Cells(r, 1).Value = folder
                r = r + 1
            End If
        End If
        folder = Dir()          '次のフォルダへ
    Loop
End Sub
VB
  • ポイント:
    • Dir(path, vbDirectory) → 最初のフォルダを取得。
    • 2回目以降は Dir()(引数なし)で次を取得。
    • GetAttr で「フォルダかどうか」を判定。

方法②:FileSystemObjectで一覧(可読性が高い)

Sub ListFolders_FSO()
    Dim fso As Object, folder As Object, subFolder As Object, r As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("C:\Data")

    r = 2
    For Each subFolder In folder.SubFolders
        Cells(r, 1).Value = subFolder.Name
        Cells(r, 2).Value = subFolder.Path
        r = r + 1
    Next
End Sub
VB
  • ポイント:
    • SubFolders コレクションで一気に列挙できる。
    • Name はフォルダ名、Path はフルパス。
    • 可読性が高く、初心者でも理解しやすい。

応用:サブフォルダを再帰的に取得(階層全部)

Sub ListFolders_Recursive()
    Dim fso As Object, root As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set root = fso.GetFolder("C:\Data")

    Call ShowSubFolders(root, 2)
End Sub

Sub ShowSubFolders(ByVal folder As Object, ByVal r As Long)
    Dim subFolder As Object
    For Each subFolder In folder.SubFolders
        Cells(r, 1).Value = subFolder.Path
        r = r + 1
        '再帰呼び出しでさらに下の階層へ
        r = ShowSubFolders(subFolder, r)
    Next
End Sub
VB
  • ポイント:
    • 再帰処理で階層を潜って全フォルダを取得。
    • ネットワークや大量フォルダでは時間がかかるので注意。

例題で練習

例題1:指定フォルダ直下のサブフォルダ名を一覧化

Sub Example_ListSubFolders()
    Dim fso As Object, folder As Object, subFolder As Object, r As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(ThisWorkbook.Path) 'このブックの場所
    r = 2
    For Each subFolder In folder.SubFolders
        Cells(r, 1).Value = subFolder.Name
        r = r + 1
    Next
End Sub
VB

例題2:サブフォルダのフルパスを一覧化

Sub Example_ListFullPath()
    Dim fso As Object, folder As Object, subFolder As Object, r As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("C:\Users\Public")
    r = 2
    For Each subFolder In folder.SubFolders
        Cells(r, 1).Value = subFolder.Path
        r = r + 1
    Next
End Sub
VB

例題3:サブフォルダ一覧を作成し、存在チェック付き

Sub Example_ListFoldersWithCheck()
    Dim path As String: path = "C:\Data"
    If Dir(path, vbDirectory) = "" Then
        MsgBox "フォルダが存在しません: " & path, vbExclamation
        Exit Sub
    End If

    Dim fso As Object, folder As Object, subFolder As Object, r As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(path)
    r = 2
    For Each subFolder In folder.SubFolders
        Cells(r, 1).Value = subFolder.Name
        Cells(r, 2).Value = subFolder.DateCreated
        r = r + 1
    Next
End Sub
VB

落とし穴と対策

  • 「.」「..」が混ざる: Dir関数では必ず除外。
  • フォルダとファイルの混在: GetAttrでフォルダ判定、FSOなら SubFolders で安全。
  • 大量フォルダで遅い: 再帰処理は時間がかかる。必要に応じて上限を設ける。
  • 権限不足: ネットワークやシステムフォルダはアクセス拒否されることがある。On Errorでガード。
  • 相対パスの混乱: 配布用は ThisWorkbook.Path を基準に組み立てると安定。
タイトルとURLをコピーしました