用vba 程序搜索文件夹
时间: 2024-04-29 19:24:58 浏览: 17
的代码应该怎么写?
你好!一下是回答:
可以使用以下代码来搜索文件夹:
```
Sub SearchFolder()
Dim fso As Object
Dim folderPath As String
Dim keyword As String
Dim folder As Object
Dim subFolder As Object
Dim file As Object
'设置搜索路径和关键字
folderPath = "C:\SearchFolder"
keyword = "example"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
'搜索当前文件夹
For Each file In folder.Files
If InStr(file.Name, keyword) > 0 Then
Debug.Print file.Path
End If
Next
'搜索子文件夹
For Each subFolder In folder.SubFolders
SearchFiles subFolder.path, keyword
Next
Set fso = Nothing
End Sub
```
希望能帮到你!