EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA File & Directories Functions VBA Check File Exists
 

VBA Check File Exists

Jeevan A Y
Article byJeevan A Y
Madhuri Thakur
Reviewed byMadhuri Thakur

VBA Check File Exists

Check File Exists Using Excel VBA

VBA Check File Exists helps to Check if file Exists in Location using Excel VBA. After mentioning the file path in the computer, what if someone deletes the file or change the folder path of the file? Obviously, our code will throw an error in such cases. To resolve this issue, we can do one thing before we actually open the file, we can check whether the mentioned file exists or not.

 

 

In this article, we will show you how to check whether the particular mentioned file exists or not.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Check if File Exists or Not?

  • How excel VBA knows whether the file exists or not??
  • By default, it cannot!!!
  • So, then how??
  • We need to use the function called “Dir” to check whether the file exists or not.

What does DIR Function Do?

VBA DIR function returns the name of the file name with its extension in the specified folder path. When the folder doesn’t have any file, it returns the empty string.

So by using this function, we can actually test whether the file exists or not. Even without the DIR function, we can test whether the file exists or not. We will see some of the examples below.

How to Use VBA Check File Exists in Excel?

We will learn how to use a VBA Check File Exists Function with few examples in excel.

You can download this VBA Check File Exists Excel Template here – VBA Check File Exists Excel Template

Example #1 – VBA Check File Exists

Ok, let’s write some code to test the file exists or not. Follow the below steps to write code on your own.

Step 1: For this, go to the VBA window and under the Insert menu select Module as shown below.

VBA Check File Exists Module

Step 2: Start the subprocedure.

Code:

Sub File_Example()

End Sub

VBA Check File Exits Example 1.1

Step 3: Define the variable as String.

Code:

Sub File_Example()

Dim FilePath As String

End Sub

VBA Check File Exits Example 1.2

Step 4: Now, I want to test the file named as “Chapter-11. InputBoxes.xlsm” in my E-Drive”. I will assign my file path to this variable.

Code:

Sub File_Example()

Dim FilePath As String

FilePath = "D:\Test File.xlsx"

End Sub

 Example 1.4

Step 5: Now define one more variable to apply the DIR function.

Code:

Sub File_Example()

Dim FilePath As String
Dim FileExists As String

FilePath = "D:\Test File.xlsx"

End Sub

Example 1.5

Step 6: Now, for the second variable, open the DIR function.

Code:

Sub File_Example()

Dim FilePath As String
Dim FileExists As String

FilePath = "D:\Test File.xlsx"
FileExists = Dir(

End Sub

Example 1.7

Step 7: DIR function requires the file path. Since we have already assigned the file path to the variable “FilePath”, we can simply pass this variable to the DIR function.

Code:

Sub File_Example()

Dim FilePath As String
Dim FileExists As String

FilePath = "D:\Test File.xlsx"
FileExists = Dir(FilePath)

End Sub

Example 1.8

Step 8: Now DIR function returns only the File Name as “Chapter-11. InputBoxes” from the mentioned file path. So let’s show the result in a message box.

Code:

Sub File_Example()

Dim FilePath As String
Dim FileExists As String

FilePath = "D:\Test File.xlsx"
FileExists = Dir(FilePath)

MsgBox FileExits

End Sub

Example 1.9

Step 9: Now run the macro to see the result.

VBA Check File Exits 1

Since there is a file that exists in the mentioned path, our DIR function filtered the file name from the huge path.

Step 10: Now, I will change the file name to a different thing which is not there in the mentioned path.

Code:

Sub File_Example()

Dim FilePath As String
Dim FileExists As String

FilePath = "D:\ File.xlsx"
FileExists = Dir(FilePath)

MsgBox (FileExists)

End Sub

 Example 1.10

Step 11: If I run the code now, it will return an empty string in the message box.

Example 1.11

DIR function is best suited to use with the IF statement in VBA. Above, we could see only the file name with its extension if it exists, or else we could only see the empty string.

Example #2 – DIR with IF Condition

Step 1: But using the IF statement with the DIR function, we can alter our results. For an example, look at the below code.

Code:

Sub File_Example1()

Dim FilePath As String
Dim FileExists As String
FilePath = "C:\Users\cba_28\Desktop\Alex\Final Input.xlsm"

FileExists = Dir(FilePath)

     If FileExists = "" Then
MsgBox "File doesnt exists in the mentioned path"
     Else
MsgBox "File exists in the mentioned path"
End If

End Sub

VBA Check File Exits Example 2.1

Step 2: Here IF condition checks whether the variable “FileExists” value is nothing (“”) or not. If the variable value is nothing (“”), then it will return the result as “File doesn’t exist in the mentioned path”, or else it will return the result as “File exists in the mentioned path.”

Below is an example of a screenshot of the same.

VBA Check File Exits Example 2.2

Example 2.3

By using the DIR function, we can check whether the file exists or not.

Recommended Articles

This is a guide to VBA Check File Exists. Here we discuss how to use Excel VBA Check File Exists Function along with practical examples and a downloadable excel template. You can also go through our other suggested articles –

  1. VBA Copy Paste
  2. VBA Web Scraping
  3. VBA Subscript out of Range
  4. VBA RGB

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Check File Exists Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW