- ©«¤l
- 109
- ¥DÃD
- 2
- ºëµØ
- 0
- ¿n¤À
- 114
- ÂI¦W
- 0
- §@·~¨t²Î
- Win7 Win10
- ³nÅ骩¥»
- Office 2019 WPS
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ²`¦`
- µù¥U®É¶¡
- 2013-2-2
- ³Ì«áµn¿ý
- 2024-11-6
|
¦^´_ 7# ui123 - '¨Ï¥ÎFSO·j¯Á¤å¥ó©Î¬O¤å¥ó夹
- Public Function FSOFileSearch(Optional ByVal SearchPath As String, _
- Optional ByVal objFolder As Object = Nothing, _
- Optional ByVal SearchName As String = vbNullString, _
- Optional ByVal SearchSub As Boolean = True, _
- Optional ByVal SearchType As Long = 1) As Collection
- Dim FSO As Object
- Dim Filter() As String
- Dim subSearch As Collection
- Dim I As Long, J As Long
- Dim objSub As Object
-
- 1 Set FSOFileSearch = New Collection
- 2 If objFolder Is Nothing Then
- 3 If Len(SearchPath) = 0 Then Exit Function
- 4 Set FSO = CreateObject("Scripting.FileSystemObject")
- 5 Set objFolder = FSO.GetFolder(SearchPath)
- 6 Set FSO = Nothing
- 7 If objFolder Is Nothing Then Exit Function
- 8 End If
-
- 9 If Len(SearchName) < 1 Then SearchName = "*.*|*"
- 10 Filter = Split(Replace(SearchName, "|", vbNullChar), vbNullChar)
- 11 If Len(SearchPath) Then
- 12 For I = 0 To UBound(Filter)
- 13 Filter(I) = LCase$(Trim$(Filter(I)))
- 14 If Len(Filter(I)) = 0 Then
- 15 If I = UBound(Filter) Then Exit For
- 16 For J = I + 1 To UBound(Filter)
- 17 If Len(Filter(J)) Then
- 18 Filter(I) = Filter(J)
- 19 Filter(J) = vbNullString
- 20 I = J - 1
- 21 Exit For
- 22 End If
- 23 Next J
- 24 If J > UBound(Filter) Then Exit For
- 25 End If
- 26 Next I
- 27 ReDim Preserve Filter(I - 1)
- 28 SearchName = Join(Filter, vbNullChar)
- 29 End If
-
- 30 If SearchType And 1& Then
- 31 For Each objSub In objFolder.Files
- 32 With objSub
- 33 For I = 0 To UBound(Filter)
- 34 If LCase(.Name) Like Filter(I) Then
- 35 FSOFileSearch.Add .Path
- 36 Exit For
- 37 End If
- 38 Next I
- 39 End With
- 40 Next objSub
- 41 End If
-
- 42 If SearchType And 2& Then
- 43 For Each objSub In objFolder.SubFolders
- 44 With objSub
- 45 For I = 0 To UBound(Filter)
- 46 If LCase(.Name) Like Filter(I) Then
- 47 FSOFileSearch.Add .Path
- 48 Exit For
- 49 End If
- 50 Next I
- 51 End With
- 52 Next objSub
- 53 End If
-
- 54 If SearchSub Then
- 55 For Each objSub In objFolder.SubFolders
- 56 DoEvents
- 57 Set subSearch = FSOFileSearch(, objSub, SearchName, SearchSub, SearchType)
- 58 With subSearch
- 59 For J = 1 To .Count
- 60 FSOFileSearch.Add .Item(J)
- 61 Next J
- 62 End With
- 63 Set subSearch = Nothing
- 64 Next objSub
- 65 End If
- End Function
- Sub TestSearch()
- Dim I As Long
- Dim Search As Collection
-
- Set Search = FSOFileSearch(ThisWorkbook.Path, , [A1], True, 1)
- For I = 1 To Search.Count
- Workbooks.Open Search(I)
- Next I
- End Sub
½Æ»s¥N½X nª`·Nªº¬O¦pªGA1单¤¸®æ¤¤ªº内®e¥]§t¤Ç°t¦r²Å¡A将会³Q¦p´Á¨Ï¥Î¡A¤ñ¦pA1¤¤输¤J*.*|*将会·j¯Á¥X«ü©w¸ô径¤¤ªº©Ò¦³¤å¥ó¡A*.XLS[XM]则ªí¥Ü·j¯Á¦Z缀为.XLSX¤Î.XLSM¤å¥ó¡]ÉO*.XLSX|*.XLSM¬Û¦P¡A|ªí¥ÜOr)¡CA1¤¤ªº内®e¥u¯à¥]§t¤å¥ó¦W¡A¦Ó³Ì¦n¤£n¥]§t¥Ø录¦W¡C |
|