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
1. Open an Excel file
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Check File Exist’
7. Right click on the shape and select ‘Assign Macro…’
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:
Introduction to Power Query Hi Friends, we are starting with a new series of articles on a very important feature of MS Excel. Yes, that is Power Query. It was first introduced with Excel 2010…
Table of Content VBA Code to Get User Domain Name VBA Code to Get User Domain Name – Method 1 VBA Code to Get User Domain Name – Method 2 Steps to use this VBA…
https://youtu.be/8Qkc1vt8_nQ Are you struggling with merging columns in Power Query? Don’t worry! In this comprehensive guide, we’ll walk you through the process step by step so you can easily merge columns in Power Query. Step…
VBA Code To Delete All Shapes On A Excel sheet Here is a VBA code which deletes all the shapes from an Excel sheet. Code is simple but you have to be bit careful while…
Introduction Welcome to this comprehensive guide on using Power Query in Excel to split data efficiently. In this tutorial, we’ll walk you through the step-by-step process of splitting columns, specifically focusing on splitting the name…
Lock Cells to avoid editing, Hide Formulas Sometimes you create amazing projects, dashboards however people can make mistakes and edit the calculations, formats what you have made. Still you can protect the sheets, workbooks and…
GOOD IDEA