To ensure that your VBA project works smoothly without any error, you need to put lot of error handlers and validations. Here is one of them, below code can be used to check if the given path is valid or not. You just need to provide the path of the file to the function and it will check if the file exist at the location. If it returns True that means the file is available at the path, if it returns False that means it is invalid path or the file does not exist at the location.
'This function checks if given file path is valid or not
'If the function returns True that means file exist
'If the function retunrs False that means file does not exist at given path
Sub CheckFileExist()
Dim lAttributes As Long
Dim strFilePath As String
'Read the file path from Cell B4 and assign to the variable
strFilePath = Sheet1.Range("B4").Value
'Include read-only, hidden and system files
lAttributes = (vbReadOnly Or vbHidden Or vbSystem)
'If last character is slash then remove the same
Do While Right(strFilePath, 1) = "\"
strFilePath = Left(strFilePath, Len(strFilePath) - 1)
Loop
'If Dir returns anything that means the file exist
On Error Resume Next
If (Len(Dir(strFilePath, lAttributes)) > 0) Then
MsgBox "File exist at the location", vbInformation
Else
MsgBox "File does not exist at the location", vbCritical
End If
On Error GoTo 0
End Sub
8. Select ‘CheckFileExist’ from the list and click on ‘Ok’ button
9. Done, enter a file path in cell B4 and click on the shape
Hope you liked this article !!
Subscribe our blog for new amazing excel tricks.
Click to below for some more interesting tricks and learning:
Please leave your valuable comments in Comments section:
Duplicate Files Finder Tool is an MS Excel based tool to identify duplicate files exist in your system or shared folder. The tool takes a base folder path as input and list down all duplicate files in the folder and sub-folders. It has capability to find duplicate files by comparing the names and their size.
Excel VBA Tool To Get File Properties Here is one more interesting VBA tool from the ExcelSirJi team. File Properties Tool is an Excel VBA tool that gets the following properties of the file. File…
In this article we are going to show you how you can delete file or files using a single line of VBA code. 1. Delete a specific file from the folder, 2. Delete specific type of files from the folder, 3. Delete all files from the folder
How to use VBA to open Workbook in Excel? There are few VBA codes which are commonly used by every developer. One of them is giving an option to user to browse a file. Below is a…
VBA Code to send Outlook Emails Sending bulk emails is a very common activity, there are many office activities that need a person to send bulk emails to single or multiple recipients. You also may…
VBA Code to Read Outlook Emails Reading emails from Outlook and capture them in Excel file is very common activity being performed in office environment. Doing this activity manually every time is quite boring and…
GOOD IDEA