Find Folder Name VBA Code

Guard913

Board Regular
Joined
Apr 10, 2016
Messages
144
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
I have tried several codes to get this to work with no success.
I have a folder that has subfolders (A-Z) that will mostly be formatted as "Name - P1111####" however sometimes it will just be "P1111####" ####= random numbers

I need a code that will use the value from a textbox in the userform (in this case "P1111####") search all the subfolders in the main directory and come back and tell me if a folder containing that text was found. Regardless if the name is there or not. *Bonus points for opening the containing folder "Not the file(s)*

If it's not found then advise that well.

Basically, the same way windows search does it when you type info into the search bar it just locates a folder containing that text regardless of extra text.

The folders have been Separated A-Z based on the first letter in the "Name" but really I don't care about searching the "Name" it's mostly there to help verify that the number matches the correct account.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Find Folder Name VBA Code
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Since I had done most of the work before Joe4 posted, I did finish this :
VBA Code:
Sub findfolders()
Dim srchstring As String
srchstring = "galp3b"

Dim tt As String
Dim ts As Variant
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim MyFolder As Object
Dim MySubFolder As Object

'    Dim fso As New FileSystemObject
'    Dim MyFolder As Folder
'    Dim mySubFolder As Folder

pathn = ThisWorkbook.Path
'MsgBox pathn

Set MyFolder = fso.GetFolder(pathn)
 For Each MySubFolder In MyFolder.subfolders
   tt = MySubFolder.Name
   ts = InStr(1, tt, srchstring, 1)
   If ts > 0 Then
    MsgBox (tt)
    Shell "Explorer.exe " & MySubFolder, vbNormalFocus
    Exit Sub
   End If
 '  Debug.Print MySubFolder.Name
 Next MySubFolder
 
End Sub
Obviously you can set the search string by any means you wnat.
 
Upvote 0
Couldn't get looping through subfolders to work so go rid of the subfolders and used this code instead, and it does what I need just can't have my files organized as well. Thanks for the help.


VBA Code:
Sub Main_Search()
   
   Dim FName As String
    Dim strSearch As String
    Dim fPath As String
    
    'What do you want to find?
    strSearch = Scrubs_Form.OrderNumber_Scrubs.Value
    'What main folder to search?
    fPath = "H:\S&B\Daily\(1) Old"
    
    'Error check
    If Right(fPath, 1) <> Application.PathSeparator Then
        fPath = fPath & Application.PathSeparator
    End If
    
    'See if any FOLDER matches
    FName = Dir(fPath & "*" & strSearch & "*", vbDirectory)
    
    If FName = "" Then
        Tools_Form.Folder_Look_Up.Value = "No Folder Found"
        Tools_Form.File_Path_X.Value = "Create New Folder"
    Else
        Tools_Form.Folder_Look_Up.Value = "Folder Found: " & FName
        Sheets("Sheet1").Range("G14") = FName
        Tools_Form.File_Path_X.Value = Worksheets("Sheet1").Range("G15").Text
        Tools_Form.Label1665.Caption = "Move"
  
    End If
 
Upvote 0

Forum statistics

Threads
1,213,408
Messages
6,113,538
Members
448,494
Latest member
alecto3

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