Yes. In VBA, use Dir$ with the workbook’s full file path and check whether it returns an empty string: an empty result means no matching file was found. This checks for a file on disk; it does not tell you whether the workbook is already open in Excel, which requires a separate check.
What “workbook exists” can mean
The right check depends on what you want to know. A workbook can exist as a saved file, be open in the current Excel session, or be available at a cloud location that VBA cannot treat as an ordinary local file path. Those are related but different conditions.
- File exists: The named file can be found at the specified path. Use
Dir$for a straightforward Windows file-system check. - Workbook is open: The workbook is loaded in the current Excel instance. Check the
Workbookscollection. - Workbook is accessible: Excel can actually open or read it. A file can exist but still be inaccessible because of permissions, a network problem, or a damaged file.
Being precise about the question prevents a common mistake: checking whether a file exists and assuming that proves it can be opened. The check is useful before opening a file, but the open operation can still fail.
Check a file with Dir$
Dir$ searches for a file matching the path you provide. For a full path to a particular workbook, it returns the matching file name when found and an empty string when no match is found. The dollar-sign form returns a String directly, which is convenient in VBA.
This example includes normal, read-only, hidden, and system files in the search. Put the function in a standard VBA module, such as one created with Insert > Module in the Visual Basic Editor.
Before running a macro, save a copy of the workbook: VBA macros cannot be undone with Ctrl+Z, even though this particular check does not edit cells.
Public Function WorkbookFileExists(ByVal fullPath As String) As Boolean
Dim foundName As String
' An empty path cannot identify a workbook file.
If Len(Trim$(fullPath)) = 0 Then Exit Function
foundName = Dir$(fullPath, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)
WorkbookFileExists = (Len(foundName) > 0)
End Function
The function returns True if Dir$ finds a match and False if it does not. The blank-path guard avoids searching with an empty value. The attribute flags help include files marked read-only, hidden, or system; they do not make the function a test of whether Excel can open the file.
Use the function before opening a workbook
Pass a full path, including the drive and file name, to the function. For example, the following macro checks for a workbook in a Reports folder and gives a different message for each result.
Save a copy before running the macro; if it is changed later to make edits, Ctrl+Z will not reverse those VBA changes.
Sub CheckReferenceWorkbook()
Dim fullPath As String
' Set the complete path to the file being checked.
fullPath = "C:\Reports\April.xlsx"
If WorkbookFileExists(fullPath) Then
MsgBox "The workbook file exists."
Else
MsgBox "No matching workbook file was found."
End If
End Sub
Change the example path to the real location. A relative name such as April.xlsx is not a reliable substitute: the result depends on the current working directory, which may not be the folder you expect. An absolute path makes the target explicit.
Build a path beside the current workbook
If the file to check should be in the same folder as the macro workbook, use ThisWorkbook.Path as the folder. ThisWorkbook means the workbook containing the running VBA code; it is not necessarily the workbook the user most recently selected. Check that the macro workbook has been saved first, because its path is empty before its first save.
On Windows, a path can be assembled with Application.PathSeparator, which supplies the separator for the current Excel platform:
Sub CheckSiblingWorkbook()
Dim folderPath As String
Dim fullPath As String
' Save a copy before running any macro.
folderPath = ThisWorkbook.Path
If Len(folderPath) = 0 Then
MsgBox "Save this workbook before checking its folder."
Exit Sub
End If
fullPath = folderPath & Application.PathSeparator & "April.xlsx"
If WorkbookFileExists(fullPath) Then
MsgBox "April.xlsx is in the same folder."
Else
MsgBox "April.xlsx was not found in the same folder."
End If
End Sub
For Excel for Microsoft 365 on Windows, the separator is a backslash. Excel for Mac has different file-system conventions, so use the platform-provided separator rather than typing a separator into code intended to run on both systems. VBA and file-system behavior can also vary for cloud URLs; a SharePoint or OneDrive web address is not necessarily a normal local path that Dir$ can search.
Check whether the workbook is already open
Dir$ looks for a file on disk, not for a workbook already loaded in Excel. To check the current Excel instance, loop through Application.Workbooks. Comparing full paths is safer than comparing only names: two different folders can contain workbooks with the same file name.
This function uses a case-insensitive comparison, appropriate for ordinary Windows paths. Save a copy before running a macro, and remember that VBA changes cannot be reversed with Ctrl+Z.
Public Function IsWorkbookOpen(ByVal fullPath As String) As Boolean
Dim wb As Workbook
' Compare each open workbook's complete path.
For Each wb In Application.Workbooks
If StrComp(wb.FullName, fullPath, vbTextCompare) = 0 Then
IsWorkbookOpen = True
Exit Function
End If
Next wb
End Function
This checks only workbooks open in the Excel instance running the code. A file open in a separate Excel process will not appear in this instance’s Workbooks collection. If you need to open the file, first consider whether it is already open; otherwise Excel may open another copy or report a sharing conflict.
Common limitations and errors
A missing file is only one reason a check may not produce the result you expect. Consider these cases when deciding what to do after the test:
- Incorrect or incomplete path: Confirm the drive, folder names, extension, and spelling. A path that omits the folder may search somewhere other than the workbook’s location.
- Network location unavailable: A disconnected drive or unreachable network share can prevent a useful result. Restore the connection and check again; do not treat every failure as proof that someone deleted the file.
- Access restrictions: A file can be listed but still be blocked from opening because the user lacks permission or another process has locked it.
- Unsaved current workbook:
ThisWorkbook.Pathis empty until the code-containing workbook has been saved. - Cloud-only or URL path: A web address is not equivalent to a local Windows path. A synced OneDrive file may have a local path, but an online URL may need a different approach.
Also avoid using Dir$ for unrelated searches inside the same routine without care. VBA’s Dir function keeps search state when used to enumerate multiple matches; a later call can replace that state. For a single full-path existence check this is usually simple, but code that loops through many files should manage its searches deliberately.
Dir$ compared with FileSystemObject
For a basic existence test, Dir$ is usually the shortest option and does not require creating an extra object. Another Windows option is the FileSystemObject, whose FileExists method expresses the intent clearly. It can be useful in code that already uses FileSystemObject methods for folders or file operations.
With late binding, a Windows VBA procedure can create the object without setting a reference in the VBA editor. However, this adds a COM dependency and is less suitable for code that must work across platforms. For a simple check in an Excel macro, Dir$ is typically easier to deploy. Neither method proves that a workbook is valid Excel content or that the current user can open it successfully.
Handle the open operation as a separate step
There is a small but important gap between checking and opening: the file could be moved, deleted, or made unavailable after the existence check. Therefore, treat the check as a helpful decision point, not a guarantee. If opening the workbook is the real goal, handle errors from Workbooks.Open as well and show a useful message rather than allowing an unexpected failure to interrupt the macro.
For example, after confirming that the path is nonblank and the file appears to exist, open it with an error handler appropriate to your workflow. Do not suppress every error without explanation: a permission problem and a damaged workbook need different responses from a simple “not found” result. Keep the original file untouched until you know the macro’s next steps are safe.
Frequently Asked Questions
How do I check whether a workbook exists in a specific folder?
Give WorkbookFileExists the complete path, such as C:\Reports\April.xlsx. It returns True when the file is found and False when no matching file is found.
Does Dir$ check whether a workbook is open?
No. Dir$ checks the file system. To check whether the workbook is loaded in the current Excel instance, inspect Application.Workbooks and compare the workbook’s full path or name.
Can I check a workbook in the same folder as my macro?
Yes. Combine ThisWorkbook.Path, Application.PathSeparator, and the file name. Save the macro workbook first, because ThisWorkbook.Path is blank for a workbook that has never been saved.
Why does the check say a file is missing when I can see it?
Often the code is checking a different folder, using a typo or wrong extension, or looking for a network or cloud path that is unavailable in that form. Display the exact path being checked and verify it in File Explorer.
Does a True result mean Excel can open the workbook?
No. It means a matching file was found. Permissions, file locks, corruption, or a change between the check and the open attempt can still prevent Excel from opening it.
Related
- Microsoft Excel Scenario Manager: Setup, Uses, and Limits
- Microsoft Excel: Remove Page Break Lines and Hide Them
- Microsoft Excel Scatter Plot: How to Create and Read One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Move a Picture in an Excel UserForm with the MouseTo move a picture around an Excel UserForm with the mouse, handle the Image control’s MouseDown and MouseMove…
- Excel VBA Active Range: What Selection Really MeansExcel VBA does not have a built-in ActiveRange property. To refer to the range currently selected in Excel,…
- Free Windows Macro Recorder: Options and How They WorkYes. You can record repetitive Windows tasks for free with Microsoft Power Automate for desktop, or use free…
- Excel VBA Copy and Paste Formatting: Code and TipsTo copy only formatting with Excel VBA, copy the source range and use PasteSpecial with xlPasteFormats: sourceRange.Copy, then…
- Excel VBA Copy and Paste Values OnlyTo copy a range in Excel VBA and paste only its values, use Copy followed by PasteSpecial Paste:=xlPasteValues.…
- Excel VBA: How to Check Whether a Workbook Is OpenTo check whether a workbook is open in Excel VBA, loop through Application.Workbooks and compare each open workbook’s…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.