Search for a folder which contains data from a cell

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
514
I have a cell B2 in my spreadsheet that contains a four digit number (or a four digit number with a letter). I have another cell that has the starting directory to begin searching (cell Z2). The folder names in this directory have the names with the number listed in B2 along with additional text (e.g. = "4453 Balloon Central"). I would like to have a macro search the root directory (file path in Z2), for folder names with the text in cell B2, and then paste the folder name in another cell (Z4). I can then use that text to generate a complete file pathway to save a file. I do not even know where to start with this one, so I would appreciate any direction with this one.

Thanks,

Robert
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,870
Office Version
  1. 2007
Platform
  1. Windows
Put all the code in a module.
Run the macro: Search_for_a_folder.
Results in cell Z4 and down

VBA Code:
Option Explicit
Dim xfolders As New Collection
Dim i As Long

Sub Search_for_a_folder()
  Dim sPath As String
  Range("Z4:Z" & Rows.Count).ClearContents
  i = 4
  Call AddSubDir(Range("Z2").Value)
End Sub
'
Sub AddSubDir(lPath As Variant)
  Dim SubDir As New Collection, DirFile As Variant, sd As Variant
  If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
  DirFile = Dir(lPath & "*", vbDirectory)
  Do While DirFile <> ""
    If DirFile <> "." And DirFile <> ".." Then
      If ((GetAttr(lPath & DirFile) And vbDirectory) = 16) Then
        If InStr(1, DirFile, Range("B2").Value, vbTextCompare) Then
          Range("Z" & i).Value = lPath & DirFile
          i = i + 1
        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
 
Last edited:

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
514
Dante,

That code works great and finds the folder name within seconds, the problem now is that the macro runs and does not stop. I have to hit the escape key to get the macro to quit running. If I let it run out completely, it will add several lines to the first one based on what it finds. I would like it to stop after it finds the first iteration. I do not need it to go into the first directory folder and identify additional folders with the first one. I may get the following examples:

M:\myfiles\4453 Balloon Central\
M:\myfiles\4453 Balloon Central\Standard Order\
M:\myfiles\4453 Balloon Central\Color Preference\Ribbon Preference
M:\myfiles\4453 Balloon Central\Shipping location

I would like it to stop at the first one.

M:\myfiles\4453 Balloon Central\

What do I need to do to fix it?
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,870
Office Version
  1. 2007
Platform
  1. Windows
After this line:
Range("Z" & i).Value = lPath & DirFile

Put this line:
End
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,870
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,133,538
Messages
5,659,384
Members
418,500
Latest member
Guru Prasad S

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
Top