VBA Check File Exists

In this article I will explain how you can check if a file exists using VBA.

Lets say we have the file “TempFile.xlsx” in the path “D:StuffBusinessTemp”:

VBA, Delete file

The code below will check if the file with the full path “D:StuffBusinessTempTempfile.xlsx” exists or not. An appropriate message box will be displayed if it does:

Sub Example1()
Dim ObjFso As Object
Dim strPath As String
Dim CheckExists As Boolean
'file path
strPath = "D:StuffBusinessTempTempfile.xlsx"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'deletes file
CheckExists = ObjFso.FileExists(strPath)
If CheckExists = True Then
    MsgBox ("The file exists")
Else
    MsgBox ("The file does not exist")
End If
End Sub

Result:

Result File Exists
The function below returns True if the file specified by the input path exists and false if it doesn’t:

CheckExists = ObjFso.FileExists(strPath)

You can download the file and code related to this article from the link below:

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

One thought on “VBA Check File Exists”

Leave a Reply

Your email address will not be published. Required fields are marked *