Loop through files in a folder using VBA?

后端 未结 6 2305
南笙
南笙 2020-11-21 04:40

I would like to loop through the files of a directory using vba in Excel 2010.

In the loop, I will need:

  • the filename, and
  • the date at which
6条回答
  •  伪装坚强ぢ
    2020-11-21 05:06

    Here's my interpretation as a Function Instead:

    '#######################################################################
    '# LoopThroughFiles
    '# Function to Loop through files in current directory and return filenames
    '# Usage: LoopThroughFiles ActiveWorkbook.Path, "txt" 'inputDirectoryToScanForFile
    '# https://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba
    '#######################################################################
    Function LoopThroughFiles(inputDirectoryToScanForFile, filenameCriteria) As String
    
        Dim StrFile As String
        'Debug.Print "in LoopThroughFiles. inputDirectoryToScanForFile: ", inputDirectoryToScanForFile
    
        StrFile = Dir(inputDirectoryToScanForFile & "\*" & filenameCriteria)
        Do While Len(StrFile) > 0
            Debug.Print StrFile
            StrFile = Dir
    
        Loop
    
    End Function
    

提交回复
热议问题