|  

» Determine if a workbook exists using VBA in Microsoft Excel

VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
The function below can be used to determine if a workbook or another file exists:
Function FileExists(FullFileName As String) As Boolean
' returns TRUE if the file exists
    FileExists = Len(Dir(FullFileName)) > 0
End Function
Example:
If Not FileExists("C:\FolderName\SubFolder\FileName.xls") Then 
    MsgBox "The file doesn't exist!"
Else
    Workbooks.Open "C:\FolderName\SubFolder\FileName.xls"
End If



Rate This Tip
12 34 5
Rating: 4.29     Views: 41348
small error, returns the first file in the directory if FullFileName = ""

If Len(Dir(FileName)) > 0 And FileName <> "" Then
FileExists = True
Else: FileExists = False
End If
Click here to post comment
For Registered Users
Name
Comment Title
Comments