search first four or five characters of a files name for a match

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
576
Office Version
  1. 365
Platform
  1. 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.

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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If the length is 4 or 5, you can make a variable = the LEN of the number you're looking for and doing a left of the dirfile and using the variable. Then adding that in before you populate AK1.
 
Upvote 0
Solution
Got it working thanks for the suggestion. I was thinking along those lines, I just didn't know where to apply the formula.
 
Upvote 0

Forum statistics

Threads
1,226,114
Messages
6,189,051
Members
453,522
Latest member
Seeker2025

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top