Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

 How to check if a folder exists and if not create it?

Have you ever tried to check if a folder exists or not from Excel worksheet? In this article, I will talk about checking if a folder exists in a specified path, if not, the folder will be created automatically under the path.

Check if a folder exists in a specific file path with VBA code

Create the folder if not exists in a specific file path with VBA code


arrow blue right bubble Check if a folder exists in a specific file path with VBA code


The following VBA code may help you to check if a folder exists in a specific file path, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Check if a folder exists in a specific file path:

Sub Test_Folder_Exist_With_Dir()
'Updateby Extendoffice 20161109
    Dim sFolderPath As String
    sFolderPath = "C:\Users\DT168\Desktop\Test folder"
    If Right(sFolderPath, 1) <> "\" Then
        sFolderPath = sFolderPath & "\"
    End If
    If Dir(sFolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist", vbInformation, "Kutools for Excel"
    Else
        MsgBox "Folder doesn't exist", vbInformation, "Kutools for Excel"
    End If
End Sub

Note: In the above code, you should change the folder path and name C:\Users\DT168\Desktop\Test folder to your needed.

3. Then press F5 key to run this code, you will get the following results:

doc folder exist 1


arrow blue right bubble Create the folder if not exists in a specific file path with VBA code

Check if a folder exists in a file path, if not, to create it under this specific file path, the following VBA code may help you to finish this job.

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Create a folder if not exists in a file path:

Sub MakeMyFolder()
'Updateby Extendoffice 20161109
    Dim fdObj As Object
    Application.ScreenUpdating = False
    Set fdObj = CreateObject("Scripting.FileSystemObject")
    If fdObj.FolderExists("C:\Users\DT168\Desktop\Test folder") Then
        MsgBox "Found it.", vbInformation, "Kutools for Excel"
    Else
        fdObj.CreateFolder ("C:\Users\DT168\Desktop\Test folder")
        MsgBox "It has been created.", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = True
End Sub

Note: In the above code, you should change the folder path and name C:\Users\DT168\Desktop\Test folder to your needed.

3. After pasting the code, and press F5 key to run it:

(1.) If the folder exists, a prompt box will pop out as following screenshot shown:

doc folder exist 2

(2.) If the folder does not exist, it will be created under the specific path at once, and a prompt box will pop out to remind you the folder has been created, see screenshot:

doc folder exist 3


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    felipe · 1 months ago
    I'm running this macro, but in the step to create the folder, the process goes down.

    can you help me????


    'Comprobar si la carpeta existe

    Dim ruta As String
    Dim libro As String

    M = ActiveWorkbook.Name

    ruta = Application.Workbooks(M).Sheets("Diccionario").Range("B5").Value

    If Right(ruta, 1) <> "\" Then
    ruta = ruta & "\"
    End If
    If Dir(ruta, vbDirectory) <> vbNullString Then
    MsgBox "Folder exist, please continue"
    Else
    MsgBox "Folder doesn't exist"
    End If

    'Crea la carpeta que necesitas

    Dim fdObj As Object
    Dim folder As String

    folder = Application.Workbooks(M).Sheets("Dicionario").Range("B5").Value (here is where the process falls)

    Application.ScreenUpdating = False
    Set fdObj = CreateObject("Scripting.FileSystemObject")
    If fdObj.FolderExists(folder) Then
    MsgBox "Found it, pleace continue."
    Else
    fdObj.CreateFolder (folder)
    MsgBox "It has been created."
    End If
    Application.ScreenUpdating = True
  • To post as a guest, your comment is unpublished.
    Kyffer · 4 months ago
    Super Thanks!
  • To post as a guest, your comment is unpublished.
    Roger · 5 months ago
    Thanks Man, work amazing
  • To post as a guest, your comment is unpublished.
    Bengbeng · 6 months ago
    This is really helpful! thanks!
  • To post as a guest, your comment is unpublished.
    Kenneth · 1 years ago
    Great article. Just what I was looking for :)