|
https://club.excelhome.net/thread-1409141-1-1.html
- '*******************************************************************************************************
- '功能: 查找指定文件夹含子文件夹内所有文件名或文件夹名(含路径)
- '函数名: FileAllArr
- '参数1: Filename 需查找的文件夹名,不包含文件名
- '参数2: FileFilter 需要过滤的文件名,可省略,默认为:[*.*]
- '参数3: Liwai 剔除例外的文件名,可省略,默认为:空,一般为:ThisWorkbook.Name
- '参数4: SubFiles 是否需要查找子文件夹内文件,可省略,默认为:true
- '参数5: Files 是否只要文件夹名,可省略,默认为:FALSE
- '返回值: 一个字符型的数组
- '使用方法:FileArr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name,false,false)
- '作者: 北极狐工作室 QQ:14885553
- '*******************************************************************************************************
- Public Function FileAllArr(ByVal Filename As String, Optional ByVal FileFilter As String = "*.*", Optional ByVal Liwai As String = "", Optional ByVal SubFiles As Boolean = True, Optional ByVal Files As Boolean = False) As String()
-
- Dim DIC, DID, Ke, MyName, MyFileName
- Dim I As Long
-
- Set DIC = CreateObject("Scripting.Dictionary") '创建一个字典对象
- Set DID = CreateObject("Scripting.Dictionary")
-
- Rem 如果文件夹路径 最后不是:[\] 则补充上去
- If Mid(Filename, Len(Filename), 1) <> "" Then
- Filename = Filename & ""
- End If
-
- DIC.Add (Filename), ""
- I = 0
- Do While I < DIC.Count
- Ke = DIC.keys '开始遍历字典
- If SubFiles = True Then '//如果需要查找子文件夹
- MyName = Dir(Ke(I), vbDirectory) '查找目录
- Do While MyName <> ""
- If MyName <> "." And MyName <> ".." Then
- If (GetAttr(Ke(I) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
- DIC.Add (Ke(I) & MyName & ""), "" '就往字典中添加这个次级目录名作为一个条目
- End If
- End If
- MyName = Dir '继续遍历寻找
- Loop
- End If
- I = I + 1
- Loop
- Dim arrx() As String
- I = 0
- ReDim arrx(I)
- arrx(I) = ""
- If Files = True Then '//是否只输出文件夹名
-
- For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
- ReDim Preserve arrx(I)
- If Ke <> Filename Then '//自身文件夹除外
- arrx(I) = Ke
- I = I + 1
- End If
- Next
- FileAllArr = arrx
- Else
- For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
- MyFileName = Dir(Ke & FileFilter) '过滤器:EXCEL2003为:*.xls,excel2007为:*.xlsx
- Do While MyFileName <> ""
- If MyFileName <> Liwai Then '排除例外文件
- ReDim Preserve arrx(I)
- arrx(I) = Ke & MyFileName
- I = I + 1
- End If
- MyFileName = Dir
- Loop
- Next
- FileAllArr = arrx
- End If
- End Function
复制代码
|
|