Posted By

martinbrait on 07/29/19


Tagged

files directory vba-excel


Versions (?)

[VBA-EXCEL] Lister les fichiers d'un répertoire donné


 / Published in: Visual Basic
 

Comment lister tous les fichiers d'un répertoire dans un fichier excel ?

Excel (VBA) list all filenames in a directory

mastipro

  1. Sub ListAllFile()
  2.  
  3. Dim objFSO As Object
  4. Dim objFolder As Object
  5. Dim objFile As Object
  6. Dim ws As Worksheet
  7.  
  8. Set objFSO = CreateObject("Scripting.FileSystemObject")
  9. Set ws = Worksheets.Add
  10.  
  11. 'Get the folder object associated with the directory
  12. Set objFolder = objFSO.GetFolder("C:\")
  13. ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & "are:"
  14.  
  15. 'Loop through the Files collection
  16. For Each objFile In objFolder.Files
  17. ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
  18. Next
  19.  
  20. 'Clean up!
  21. Set objFolder = Nothing
  22. Set objFile = Nothing
  23. Set objFSO = Nothing
  24.  
  25. End Sub

Report this snippet  

You need to login to post a comment.