|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
我改成自定义函数,使用的是字典与集合.
- '改用collection形式,让弹栈更易于理解,比数组的优点是有一个明显弹栈动作,栈会变空
- Public Function vFileSearch1(pPath As String, Optional pMask As String = "", Optional pSub As Boolean = True) As Variant
- 'psb子目录开关,pPath搜索起始路径,pMask如果要必填写,那得这样填写"*.xlsx",加星号
- Dim DirFile, mf As Long, pPath1 As String, workStack As Collection, fileNameDic As Variant 'workstack工作栈,
- On Error Resume Next
- Set workStack = New Collection
- Set fileNameDic = CreateObject("scripting.dictionary")
- pPath = Trim(pPath)
- If Right(pPath, 1) <> Application.PathSeparator Then pPath = pPath & Application.PathSeparator
- pPath1 = pPath
- Do Until workStack Is Nothing
- DirFile = Dir(pPath1 & pMask)
- Do While DirFile <> ""
- fileNameDic.Add pPath1 & DirFile, pPath1 & DirFile
- DirFile = Dir
- Loop
- If pSub = False Then Exit Function
- DirFile = Dir(pPath1, vbDirectory)
- Do While DirFile <> ""
- If DirFile <> "." And DirFile <> ".." Then
- If (GetAttr(pPath1 & DirFile) And vbDirectory) = vbDirectory Then
- workStack.Add pPath1 & DirFile & Application.PathSeparator, pPath1 & DirFile & Application.PathSeparator
- End If
- End If
- DirFile = Dir
- Loop
- If workStack.Count > 0 Then
- pPath1 = workStack(workStack.Count)
- workStack.Remove (workStack.Count)
- Else
- Set workStack = Nothing
- End If
- Loop
- vFileSearch1 = WorksheetFunction.Transpose(fileNameDic.keys)
- End Function
复制代码 |
|