VBA check if file exists example Excel Macro code helps to Check if file Exists in Location using Excel VBA. You can use FileSystemObject or Dir function to check if file Exists in Location using Excel VBA.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Ultimate Project Management Template
Excel Template

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


For example, When we are automating any task we generally save the output file in a location. Using this program we can check if there is any file exists already in the same location. So that we can delete or rename the old file.

Solution(s):

You can use FileSystemObject or Dir function to check if file Exists in Location using Excel VBA.Follwoing are the examples to show you how to check If a file is already exists in a folder or not. Follwoing are the two different methods.

Check if file Exists in Location using Excel VBA – Example Cases:

Check if file Exists in Location using Excel VBA – Using FileSystemObjects

Following is the example to check if a file exists in a folder with using FileExists function of FileSystemObject.

Code:
'In this Example I am checking if Sample.xls file which exists in the same location of the macro file
Sub sbCheckingIfFileExists()

Dim FSO
Dim sFile As String

sFile = ThisWorkbook.Path & "\Sample.xls"
'OR
'"C:\Sample.xls" 'You can change this Loaction

Set FSO = CreateObject("Scripting.FileSystemObject")

If Not FSO.FileExists(sFile) Then
    MsgBox "Specified File Not Found", vbInformation, "Not Found"
Else
    MsgBox "Specified File Exists", vbInformation, "Exists"
End If

End Sub
Check if file Exists in Location using Excel VBA – Using Dir Function

Following is the example to check if a file exists in a folder with using Dir function.

Code:
'In this Example I am checking if Sample.xls file which exists in the same location of the macro file
Sub Check_If_File_Exists()

    Dim stFileName As String

    stFileName = ThisWorkbook.Path & "\Sample.xls"  'You can change this location

    If Dir(stFileName) <> "" Then
        MsgBox "Specified File Exists", vbInformation, "Exists"
    Else
         MsgBox "Specified File Not Found", vbInformation, "Not Found"
    End If

End Sub
Output:

If file exists in a location, it will show following message. Otherwise it will show “Specified File Not Found” message.
Check if file exists Using VBA

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a new module from Insert menu
  4. Copy the above code and Paste in the code window
  5. Press F5 to check the output
  6. You should see a message box as shown above
  7. Save the file as macro enabled workbook
Download Example File:

Please Download the following example file.
Analysistabs – Check if File Exists

LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template

 
Related Resource External VBA Reference