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


 How to check if a file exists in a specific folder?

Supposing, you have a folder which contains multiple files, and now, you want to identify if a specific file exists in this folder from Excel. This article, I will introduce a quick method for you to check if a file exists in a specific folder in details through Excel.

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

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

To check if a file exists in a specific folder or not in Excel worksheet, you can apply the following VBA code, 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 file exists in a specific folder:

Sub Test_File_Exist_With_Dir()
'Updateby Extendoffice 20161109
    Application.ScreenUpdating = False
    Dim FilePath As String
    FilePath = ""
    On Error Resume Next
    FilePath = Dir("C:\Users\DT168\Desktop\Test folder\Book2.xlsx")
    On Error GoTo 0
    If FilePath = "" Then
        MsgBox "File doesn't exist", vbInformation, "Kutools for Excel"
        MsgBox "File exist", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = False
End Sub

Note: In the above code, you should change the file path and name C:\Users\DT168\Desktop\Test folder\Book2.xlsx" to your need.

3. After pasting the code, please press F5 key to run this code:

(1.) If the file exists, you will get the following screenshot:

doc if file exist 1

(2.) If the file does not exist, you will get the following warning:

doc if file exist 2

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 you.

  • Designed for 1500+ work scenarios, helps you solve 80% Excel problems.
  • 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.
  • Become an Excel expert in 3 minutes. No longer need to remember any painful formulas and VBA codes.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Being used by 110,000 elites and 300+ well-known companies.

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.
  • 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.