|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub ListFilesTest() '
Application.ScreenUpdating = False '
Dim rn As Range
T = Timer '
Set d = CreateObject("Scripting.Dictionary")
With ActiveSheet
xh = 1
.[a1].CurrentRegion.Offset(1).Borders.LineStyle = 0
.[a1].CurrentRegion.Offset(1) = Empty
myPath$ = ThisWorkbook.Path & "\网银明细\"
If Right(myPath, 1) <> "" Then myPath = myPath & "" '
arr = ListAllFsoDic(myPath, 1) '
For i = 0 To UBound(arr) '
f = Dir(arr(i) & "\*.xls*") '
Do While f <> "" '
If f <> ThisWorkbook.Name Then '
xh = xh + 1
rr = Split(arr(i), "\")
.Range("a" & xh).Hyperlinks.Add Anchor:=.Range("a" & xh), Address:=arr(i) & "\" & f, TextToDisplay:=Split(f, ".")(0)
.Cells(xh, 2) = rr(UBound(rr) - 2)
.Cells(xh, 3) = rr(UBound(rr) - 1)
.Cells(xh, 4) = rr(UBound(rr))
Set wb = Workbooks.Open(arr(i) & "\" & f, 0)
Set sht = wb.Worksheets(1)
.Range("e" & xh).Hyperlinks.Add Anchor:=.Range("e" & xh), Address:=arr(i) & "\" & f, TextToDisplay:=sht.Name
Set rn = sht.Rows("1:20").Find("余额", , , , , , 1)
If Not rn Is Nothing Then
h = rn.Column
r = sht.Cells(Rows.Count, h).End(xlUp)
.Cells(xh, 6) = r
End If
wb.Close False
End If '
f = Dir '
Loop '
Next i '
.[a1].CurrentRegion.Offset(1).Borders.LineStyle = 1
End With
TT = Timer - T '
MsgBox "耗时:" & Format(TT, "0.00") & "秒!" '
Application.ScreenUpdating = True '
End Sub
Function ListAllFsoDic(myPath$, Optional k = 0) '使用2个字典但无需递归的遍历过程
Dim i&, j&
Set d1 = CreateObject("Scripting.Dictionary") '字典d1记录子文件夹的绝对路径名
Set d2 = CreateObject("Scripting.Dictionary") '字典d2记录文件名 (文件夹和文件分开处理)
d1(myPath) = "" '以当前路径myPath作为起始记录,以便开始循环检查
Set Fso = CreateObject("Scripting.FileSystemObject")
Do While i < d1.Count
'当字典1文件夹中有未遍历处理的key存在时进行Do循环 直到 i=d1.Count即所有子文件夹都已处理时停止
kr = d1.Keys '取出文件夹中所有的key即所有子文件夹路径 (注意每次都要更新)
For Each f In Fso.GetFolder(kr(i)).Files '遍历该子文件夹中所有文件 (注意仅从新的kr(i) 开始)
j = j + 1: d2(j) = f.Name
'把该子文件夹内的所有文件名作为字典Item项加入字典d2 (为防止文件重名不能用key属性)
Next
i = i + 1 '已经处理过的子文件夹数目 i +1 (避免下次产生重复处理)
For Each fd In Fso.GetFolder(kr(i - 1)).SubFolders '遍历该文件夹中所有新的子文件夹
d1(fd.Path) = " " & fd.Name & ""
'把新的子文件夹路径存入字典d1以便在下一轮循环中处理
Next
Loop
If k Then ListAllFsoDic = d1.Keys Else ListAllFsoDic = d2.Items
'如果参数=1则列出字典d1中所有子文件夹的路径名 (如使用d1.Items则仅列出子文件夹名称不含路径)
'如果参数=0则默认列出字典d2中Items即所有文件名
End Function
|
|