본문 바로가기
카테고리 없음

v2 폴더탐색 정리

by 팁텍북 2024. 7. 23.

Sub ListExcelFiles()
    Dim folderPath As String
    folderPath = "C:\Your\Folder\Path"  ' 탐색할 폴더 경로를 여기에 입력하세요.

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")
    ws.Cells.Clear

    ListFilesInFolder folderPath, ws, 1
End Sub

Sub ListFilesInFolder(folderPath As String, ws As Worksheet, ByRef row As Long)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim folder As Object
    Set folder = fso.GetFolder(folderPath)

    Dim subFolder As Object
    For Each subFolder In folder.SubFolders
        ListFilesInFolder subFolder.Path, ws, row
    Next subFolder

    Dim file As Object
    For Each file In folder.Files
        If LCase(Right(file.Name, 5)) = ".xlsx" Then
            ws.Cells(row, 1).Value = file.Path
            row = row + 1
        End If
    Next file
End Sub

댓글