|
1、在文件对话框选择要查找的文件(最高一级)
2,输入要查找的文件名的关键字,不输入的话就会得到全的文档(不包括文件夹)
代码如下:
- Option Explicit
- Dim Nrr(), Q As Long
- Sub FindFileSub()
- Dim S As String, T As String
- S = SelectFile()
- If S = "" Then Exit Sub
- T = InputBox("请输入关键字:", "查询关键字")
- T = "*" & T & "*"
- Q = 0
- DirSub S, T
- [a:b].Clear
- If Q = 0 Then Exit Sub
- With Range("a1").Resize(Q, 2)
- .Value = WorksheetFunction.Transpose(Nrr)
- .Sort key1:=[b1], order1:=2
- End With
- End Sub
- Private Sub DirSub(ByVal S As String, T As String)
- Dim S1 As String, Trr() As String, a&
- On Error Resume Next
- S1 = Dir(S & T)
- Do Until S1 = ""
- Q = Q + 1
- ReDim Preserve Nrr(1 To 2, 1 To Q)
- Nrr(1, Q) = S & S1
- Nrr(2, Q) = FileDateTime(Nrr(1, Q))
- S1 = Dir
- Loop
- a = 0
- S1 = Dir(S, vbDirectory)
- Do
- If S1 <> "." And S1 <> ".." And GetAttr(S & S1) = vbDirectory Then
- a = a + 1
- ReDim Preserve Trr(1 To a)
- Trr(a) = S & S1 & ""
- End If
- S1 = Dir
- Loop While S1 <> ""
- If a = 0 Then Exit Sub
- For a = 1 To UBound(Trr)
- DirSub Trr(a), T
- Next
- On Error GoTo 0
- End Sub
- Function SelectFile() As String '//选择文件夹。返回一个文本。
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show = True Then SelectFile = .SelectedItems(1)
- End With
- If SelectFile = "" Then Exit Function
- If Right(SelectFile, 1) <> "" Then SelectFile = SelectFile & ""
- End Function
复制代码 |
|