|
本帖最后由 香川群子 于 2013-4-18 15:19 编辑
修改了你的Dir代码:- Dim s$, fNm$, cnt&
- Sub test()
- s = InputBox("Input key word:", "Find Files", s)
- If s = "" Then Exit Sub
- pth = InputBox("Confirm FileFolder Path:", "Find Files", ThisWorkbook.Path)
- fNm = "": cnt = 0: tms = Timer
- Call ListDirs(pth)
- MsgBox Format(Timer - tms, "0.000s ") & cnt & vbCr & fNm
- ' Workbooks.Open filename:=fNm
- End Sub
- Sub ListDirs(ByVal Path As String)
- Dim i&, j&, arrPath(), sPath$, fName$
- i = 1: j = 1
- ReDim arrPath(1 To 1)
- arrPath(i) = Path & ""
- On Error Resume Next
- sPath = arrPath(j)
- Do While Len(sPath)
- cnt = cnt + 1
- fName = Dir(sPath & "*.*", vbDirectory + vbNormal)
- Do While fName <> ""
- If (fName = "." Or fName = "..") Then GoTo Nxt
- If (GetAttr(sPath & "" & fName) And vbDirectory) = 16 Then
- If Err.Number Then Err.Clear: GoTo Nxt
- i = i + 1: ReDim Preserve arrPath(1 To i)
- arrPath(i) = sPath & fName & ""
- Else
- If InStr(fName, s) Then fNm = sPath & fName: Exit Sub
- End If
- Nxt:
- fName = Dir
- cnt = cnt + 1
- Loop
- j = j + 1: If j > i Then Exit Do
- sPath = arrPath(j)
- Loop
- End Sub
复制代码 代码稍显复杂,不过Dir的效率比较高,运算速度快。
|
|