|
发表于 2016-10-16 10:44
|
显示全部楼层
本楼为最佳答案
- Sub test() '提取指定文件夹内的所有文件名
- '含所有子文件夹内的文件
- Dim Fso As Object, arrf$(), mf&
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Call GetFiles(CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹", 0, "").Self.Path, Fso, arrf, mf)
- Sheet2.[b2].Resize(mf) = Application.Transpose(arrf)
- Set Fso = Nothing
- End Sub
- Private Sub GetFiles(ByVal sPath$, ByRef Fso As Object, ByRef arrf$(), ByRef mf&)
- Dim Folder As Object
- Dim SubFolder As Object
- Dim File As Object
- Set Folder = Fso.GetFolder(sPath)
-
- For Each File In Folder.Files
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = File.Path '& File.Name
- Next
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder.Path, Fso, arrf, mf)
- Next
- Set Folder = Nothing
- Set File = Nothing
- End Sub
复制代码 |
|