Vba list all files in folder in Excel

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Sub ListAllFilesInFolder()
  3. Dim sInputFolderPath As String, sFileName As String
  4. ''
  5. sInputFolderPath = "C:\Users\Guest\Documents\TestFolder\"
  6. ''
  7. 'Get the name of the first file in folder specified.
  8. sFileName = Dir(sInputFolderPath)
  9. 'sFileName = Dir(sInputFolderPath & "*.xlsx")
  10. ''
  11. 'Loop through all files in "Input Folder".
  12. Do While sFileName <> ""
  13. MsgBox sFileName
  14. ''
  15. 'Move to the next file in folder and get its name.
  16. sFileName = Dir()
  17. Loop
  18. End Sub

Description:

a) Line 9 - Commented line indicates how to specify filters - in the commented code since "*.xlsx" is specified, search is made only for files of .xlsx type. This could be extended to other file types by using

appropriate filters.

 

You can find similar Excel Questions and Answer hereunder

1) How to do webscrapping in VBA. Here some basics

2) How can I shade alternate rows in color using VBA to make it easier to read voluminous data running into hundreds of rows?

3) How can I add a legend to a chart using VBA?

4) How can I get users to select a file for processing using my macro?

5) How do I restrict user entry in ActiveX Text Box to just numeric values?

6) How can I export a WorkSheet as a PDF using VBA?

7) How do I enter a formula in a cell using VBA (using Absolute Reference)?

8) How can I find the last used cell in a Column in VBA?

9) How can I set the fill color, font color and set number format of cell to date?

10) How can I check if a file exists in a folder using VBA?

 

Here the previous and next chapter