我获得了一个4位数的桥梁计划编号。我需要找到一个对应该桥的文件夹。文件夹名称将携带桥梁计划编号和一些其他随机文本(例如“1234-华盛顿街道”)。我已经编写了一些能够实现这一目标的代码,但速度非常慢。我想知道是否有人可以采用更有效的方式来做到这一点。感谢。
Public FSO As New FileSystemObject
Public Function FoundPlan(bridge_plan As String)
Dim objFolder As Folder
Dim planFolder As Folder
Dim Path As String
Dim i As String
Path = "G:\some\path"
'This is the directory path that carries my list of folders
Set objFolder = FSO.GetFolder(Path)
If Not Len(bridge_plan) = 4 Then
FoundPlan = ""
Exit Function
End If
'If the given plan number is anything except 4 digits, the function returns
'nothing and exits
For Each planFolder In objFolder.SubFolders
If Not InStr(planFolder, bridge_plan) = 0 Then
FoundPlan = Path & planFolder
Exit For
End If
Next planFolder
'For each subfolder in my directory I use instr to search for my number
'inside the folder path.
End Function
答案 0 :(得分:2)
试试这个。它不需要遍历每个文件夹
Public Function FoundPlan(bridge_plan As String) As String
Dim planFolder As String, Path As String
Path = "G:\some\path\"
'This is the directory path that carries my list of folders
If Not Len(bridge_plan) = 4 Then Exit Function
planFolder = Dir(Path & bridge_plan & "*", vbDirectory)
If Not planFolder = vbNullString Then FoundPlan = Path & planFolder
End Function