|
'包含子目录,输出目录全名,自己修改
Option Explicit
Sub 汇总当前文件夹及子文件夹下所有工作薄的内容()
Dim filename(), n, fso, mark, sht, i, j, m, s
Set fso = CreateObject("scripting.filesystemobject")
mark = Split(".xls .xlsx .xlsm")
Call getfilename(fso, filename, n, ThisWorkbook.Path, mark)
If n = 0 Then MsgBox "!": Exit Sub
ReDim arr(1 To n, 1 To 3) As String
For i = 1 To UBound(filename, 1)
If ThisWorkbook.FullName <> filename(i) Then
m = m + 1: s = vbNullString
For j = Len(filename(i)) To 1 Step -1
If Mid(filename(i), j, 1) = "\" Then
arr(m, 1) = Left(filename(i), j - 1)
arr(m, 2) = Mid(filename(i), j + 1)
Exit For
End If
Next
With GetObject(filename(i))
For Each sht In .Sheets
s = s & "|" & sht.Name
Next
.Close
End With
arr(m, 3) = Mid(s, 2)
End If
Next
With [a1]
.Resize(Rows.Count, UBound(arr, 2)).ClearContents
.Resize(, 3) = Split("文件夹名 薄名 表名")
.Offset(1).Resize(m, UBound(arr, 2)) = arr
End With
End Sub
Function getfilename(fso, filename, n, pth, mark)
Dim spth, t, i
If Right(pth, 1) <> "\" Then pth = pth & "\"
Set spth = fso.getfolder(pth)
For Each t In spth.Files
For i = 0 To UBound(mark)
If LCase(Right(t, Len(mark(i)))) = LCase(mark(i)) And Left(t.Name, 1) <> "~" Then
n = n + 1: ReDim Preserve filename(1 To n)
filename(n) = spth & "\" & t.Name
End If
Next
Next
For Each t In spth.subfolders
Call getfilename(fso, filename, n, t, mark)
Next
End Function |
评分
-
1
查看全部评分
-
|