Tags

, ,

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:\Stuff\Business\Temp”:

VBA, Delete file

The code below will check if the file with the full path “D:\Stuff\Business\Temp\Tempfile.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:\Stuff\Business\Temp\Tempfile.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

Advertisements