VBA 输出某个文件夹下的所有文件名称

话不多说,上代码

1、无条件输出文件夹下的文件

Sub readFile()
    Dim fo As Object
    Dim file As Variant
    Set fo = CreateObject("Scripting.FileSystemObject")
    'ThisWorkbook.Path : 目标文件夹。也可直接写文件夹地址
    For Each file In fo.getfolder(ThisWorkbook.Path).Files
        Debug.Print file.Name
continue:
    Next file
End Sub

     结果如下图 

                 

   2、输出含有【sheet】名字的文件名称

Sub readFile()
    Dim fo As Object
    Dim file As Variant
    Set fo = CreateObject("Scripting.FileSystemObject")
    For Each file In fo.getfolder(ThisWorkbook.Path).Files
        If Not (InStr(file.Name, "sheet") > 0) Then
            GoTo continue
        End If
        Debug.Print file.Name
continue:
    Next file
End Sub

结果如下图 

    

  


版权声明:本文为m0_37823115原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。