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”:
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:
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:
- Microsoft MSDN: How to: Determine if a File Exists in Visual Basic
- Excel VBA, Find and List All Files in a Directory and its Subdirectories
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”