rjplante
Well-known Member
- Joined
- Oct 31, 2008
- Messages
- 576
- Office Version
- 365
- Platform
- Windows
I have a worksheet that I enter in a four digit (3288) or four digit number preceded by the letter F (F0569). This is entered into cell and then the number is parsed out and a second cell displays a root file directory to begin searching in for that four digit or five digit number. I have included the macro I have down below, but I have run into a small problem, there is another folder name with the number 3288 in it, but it is not the directory file I am looking for. I need to restrict my search to the first 5 digits only as the folder names all start with that number. If the first 5 characters do not find a match to the number in cell AB1, then I need to move onto the next one. When it finds the correct folder, the directory file path is entered into cell AK1. How do I modify my code to accomplish this?
Thanks in advance for the help.
Thanks in advance for the help.
VBA Code:
Option Explicit
Dim xfolders As New Collection
Dim i As Long
---------------------
Sub Search_For_A_Folder()
Dim sPath As String
Sheets("Main Page").Range("AK1").ClearContents
i = 1
Call AddSubDir(Range("AH1").Value)
End Sub
'----------------
Sub AddSubDir(lPath As Variant)
Dim SubDir As New Collection
Dim DirFile As Variant
Dim sd As Variant
If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
DirFile = Dir(lPath & "*", vbDirectory)
'Do While DirFile <> ""
Do While Range("AK1").Value = ""
If DirFile <> "." And DirFile <> ".." Then
If ((GetAttr(lPath & DirFile) And vbDirectory) = 16) Then
If InStr(1, DirFile, Range("AB1").Value, vbTextCompare) Then
Range("AK1").Value = lPath & DirFile
Exit Do
End If
SubDir.Add lPath & DirFile
End If
End If
DirFile = Dir
Loop
For Each sd In SubDir
xfolders.Add sd
Call AddSubDir(sd)
Next
End Sub