30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA Check IF a Workbook Exists in a Folder (Excel File)
By Puneet Gogia (Microsoft MVP)

VBA Check IF a Workbook Exists in a Folder (Excel File)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Use the Dir function with the full file path. It returns the file name when the file exists and an empty string when it does not:

If Dir("C:\Reports\Sales.xlsx") <> "" Then
    MsgBox "File exists"
Else
    MsgBox "File not found"
End If

To check for a folder rather than a file, add vbDirectory: Dir("C:\Reports\", vbDirectory) <> "". Wildcards also work, so Dir("C:\Reports\Sales*.xlsx") tells you whether any matching file is there.

CreateObject("Scripting.FileSystemObject").FileExists("C:\Reports\Sales.xlsx") returns True or False directly. Use it inside a loop that already uses Dir to list files, because a second Dir call with a path resets that loop.

To check whether a workbook is open rather than saved in a folder, test the Workbooks collection instead:

Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks("Sales.xlsx")
On Error GoTo 0
If wb Is Nothing Then MsgBox "Sales.xlsx is not open"

When you join a folder and a file name, put a backslash between them: myFolder & "\" & myFile. Without it, every file reports as missing. Skip blank names too, because Dir("C:\Reports\") returns the first file in that folder, so an empty cell reports as found.

To check if a workbook exists in a specific folder, you can use the DIR function. DIR is a function that stands for the directory. You need to specify the path of the file along with the name and extension. If the file exists, it returns the file name; otherwise, it returns a blank value (an empty string).

Write a Code to Check IF an Excel File Exists in a Folder

Use the following steps:

code to check if an excel file exist in a folder
  1. First, specify a range or a message box to get the result of the function.
  2. Now, enter the function name “DIR” and type an opening parenthesis.
  3. After that, enter the path of the file using double quotation marks and make sure to enter the file extension along with the file name.
  4. Next, type the closing parenthesis to close the line of code.
Sub vba_Check_workbook()
MsgBox Dir("C:\Users\Dell\Desktop\book1.xlsx")
End Sub

When you run this line of code, it shows you a message box with the file name, which means the file exists in the location that you have specified. If the file doesn’t exist, the message box is empty.

In most cases, you will use DIR with an IF statement, so the code shows a clear message either way:

Sub check_file_exists()
Dim filePath As String
filePath = "C:\Users\Dell\Desktop\book1.xlsx"
If Len(filePath) > 0 And Dir(filePath) <> "" Then
MsgBox "The file exists."
Else
MsgBox "The file doesn't exist."
End If
End Sub

The Len check matters: if the path is empty, Dir(“”) doesn’t return a blank. It returns the name of a file from the current folder, so the code would say the file exists.

Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook

Check for Multiple Files IF Exist in a Folder

There could be a chance that you need to check for multiple files if they all exist in a folder. In that case, you can use the following code.

Sub vba_check_workbook()

Dim myFolder As String
Dim myFileName As String
Dim myRange As Range
Dim myCell As Range

Set myRange = Range("A1:A5")
myFolder = "C:\Users\Dell\Desktop\Data"

For Each myCell In myRange
    myFileName = myCell.Value
    If Dir(myFolder & "\" & myFileName) = "" Then
        myCell.Offset(0, 1) = "File Doesn't Exists."
    Else
        myCell.Offset(0, 1) = "File Exists"
    End If
Next myCell

End Sub
multiple files if exist in a folder

To understand this code, I have broken it into three parts.

  • In the FIRST part, you have all the variables that we need to use in the code: variables for the folder address, the file name, the range where you have all the names, and each cell of the range.
  • In the SECOND part, you have defined the range address where you have all the names of the files that you wish to check. And you have also assigned the folder path to the variable.
  • In the THIRD part, you have a FOR EACH LOOP to loop through all the cells in the range. This loop uses DIR to check each name that you have in the range in the folder. It also uses IF STATEMENT to enter a value to the corresponding cell as a remark if the file exists or not.

Note: If you want to use this code, you need to change the folder path, and you need to define the range where you have all the names of the workbooks.

Check IF Any Excel File Exists with a Wildcard

DIR also accepts wildcards. With an asterisk (*), you can check if a folder has any file of a type, like any .xlsx file, or any file whose name starts with “Sales”:

Sub check_any_excel_file()
If Dir("C:\Users\Dell\Desktop\Data\Sales*.xlsx") <> "" Then
MsgBox "At least one Sales file exists."
Else
MsgBox "No Sales file found."
End If
End Sub

Check IF a File Exists with FileSystemObject

Another way is the FileExists method of the FileSystemObject. It returns True or False, so you don’t need to compare with a blank value, and an empty path simply returns False. It works on Windows only.

Sub check_file_fso()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\Users\Dell\Desktop\book1.xlsx") Then
MsgBox "The file exists."
Else
MsgBox "The file doesn't exist."
End If
End Sub

Frequently Asked Questions

How do I check if a folder exists?

Use DIR with the vbDirectory attribute, like Dir(“C:\Users\Dell\Desktop\Data”, vbDirectory). If it returns a blank, the folder doesn’t exist. With FileSystemObject, use fso.FolderExists.

Why does DIR say a file exists when the cell is empty?

With an empty file name, the path becomes just the folder followed by a backslash, and DIR returns the first file in that folder. Skip empty cells in the loop with If myFileName <> “” Then.

Can I check if the file is already open?

DIR only tells you whether the file is in the folder. To check if a workbook is open, loop through the Workbooks collection and compare each workbook’s name.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel