|
- Dim Result
- Sub extract()
- Dim p$, f$, k&, r
- With Application.FileDialog(msoFileDialogFolderPicker) '获取用户选择文件夹的路径
- .Title = "请选择文件夹"
- .InitialFileName = ThisWorkbook.Path & "" '默认打开当前目录"
- If .Show = 0 Then MsgBox "本次提取已被取消!!": Exit Sub '如果没有选择保存路径,则退出程序
- p = .SelectedItems(1) '选择的文件路径赋值给变量P
- End With
- If Right(p, 1) <> "" Then p = p & "" '判断p的右侧是否有\,有则查找这个文件夹里的文件
- 'f = Dir(p & "*.*")
- '返回变量P指定路径下带任意扩展名的文件名
- '如果有超过一个文件存在,将返回第一个找到的文件名
- '如果一个文件都没有,则返回空
- [a:b].ClearContents '清空汇总表的A列原有数据
- [a1] = "序号" '汇总表的a1写入。。。。
- [b1] = "文件名如下:" '汇总表的b1写入。。。。
- '调用底下函数,遍历所有文件,含子文件夹
- Dim FSOLibrary As Object
- Dim FSOFolder As Object
- Dim folderName As String
- folderName = p
- Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
- Result = ""
- LoopAllSubFolders FSOLibrary.GetFolder(folderName)
- Result = Application.Transpose(Split(Left(Result, Len(Result) - 2), vbCrLf))
- Application.ScreenUpdating = False
- [b2].Resize(UBound(Result), 1) = Result
- [a2].Resize(UBound(Result), 1) = "=row()-1"
- Application.ScreenUpdating = True
- MsgBox "提取完成"
- Set FSOLibrary = Nothing
- End Sub
- Sub LoopAllSubFolders(FSOFolder As Object)
- Dim FSOSubFolder As Object
- Dim FSOFile As Object
- Dim BKFullName As String
- Dim BKFullName2 As String
- For Each FSOSubFolder In FSOFolder.subfolders
- LoopAllSubFolders FSOSubFolder
- Next
- BKFullName = ThisWorkbook.FullName: BKFullName2 = ThisWorkbook.Path & "\~$" & ThisWorkbook.Name
- For Each FSOFile In FSOFolder.Files
- If FSOFile.Path <> BKFullName And FSOFile.Path <> BKFullName2 Then
- Result = Result & FSOFile.Path & vbCrLf
- End If
- Next
- End Sub
复制代码
百度拼了个给你。
|
评分
-
1
查看全部评分
-
|