如何用excel提取文件夹中的所有文件名

2025-03-13 14:31:51
推荐回答(2个)
回答1:

Sub 提取文件名称()
Cells.Clear
        Dim str$, fso, file, i&
         With Application.FileDialog(msoFileDialogFolderPicker)
         If .Show = -1 Then
         str = .SelectedItems(1)
         If Right(str, 1) <> "\" Then
         str = str & "\"
         End If
         End If
       End With
       i = 1
       Set fso = CreateObject("scripting.filesystemobject")
       
       For Each file In fso.getfolder(str).Files
       Cells(i, 1) = file.Name
       i = i + 1
       Next
    
       
       End Sub

回答2:

给你个附件

点击按钮

输入文件夹路径

点击确定

只能在excel2007及以上版本使用