How To Search For A Folder In VBA

The purpose of this code is to tell you if you need to add a folder or not.

***You don’t need to create a new folder for some files if one exists already.

This code is helpful if you have hundreds of folders to go through, otherwise if you just have a few, you can just scan the short list with your eyeballs.

So we enter the name of the folder we want to look for, in our case it’s called “working”, and then run the code.

We’ll scan through a certain starting directory, in our case it’s called “C:\a”, and if the folder already exists, we’ll return the folder and its path, otherwise, we’ll tell the user that it’s ok to create the folder.

Here is the code:

Option Compare Database

Sub Main()
    Dim fsoFileSystem As Object
    Dim strMainFolder As String
    Dim strLookFor As String
    
    strLookFor = "working"
    strMainFolder = "C:\a"
    
    Set fsoFileSystem = CreateObject("Scripting.FileSystemObject")
    DoSubFolders fsoFileSystem.GetFolder(strMainFolder), strLookFor

    'if the code didn't find the folder, you will get this message.
    Msgbox "'" & strLookFor & "' is not found so go ahead and create it.", vbInformation

End Sub

Sub DoSubFolders(Folder, strLookFor)
    Dim objSubFolder As Object

    For Each objSubFolder In Folder.SubFolders
        
        Debug.Print "*****************************************"
        Debug.Print "SubFolder= " & objSubFolder.Name
        Debug.Print "*****************************************"
        
        If objSubFolder.Name = strLookFor Then
            MsgBox "You already have a folder called '" & strLookFor & "' at '" & objSubFolder.Path & "' . Don't add it again.", vbInformation
            
            'Exit the search
            End
            
        End If
        
        DoSubFolders objSubFolder, strLookFor
    Next
    Dim objFile As Object
    For Each objFile In Folder.Files
        ' Operate on each file
        Debug.Print "FileName= " & objFile.Name
        
    Next
End Sub

If the file is found, we get this message box:

…and if the file is not found (“working2”):

Any questions?




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide)

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide) When you work with large datasets in Excel, it’s common to end up with thousands of blank rows scattered throughout your worksheet. Maybe your data came from a mainframe export, a CSV download, or a legacy system that doesn’t format properly. Deleting all those […]

How to Copy Data from One Worksheet to Another Using VBA in Excel

Learn how to copy data between worksheets in Excel using VBA step-by-step! This quick tutorial shows how to automate data movement, save time, and boost productivity. Watch: How to Copy Data from One Worksheet to Another Using VBA Why Automate Copying Data Between Worksheets? Copying and pasting data manually can take up valuable time and […]

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How to Fix Run Time Error 1004 in Excel

If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]


Support these sponsors:
Previous Post

Converting A Excel Spreadsheet To Access Database

Next Post

How To Create A MS Access Dynamic Report Based On A Dynamic Table