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:
Colorindex in Excel VBA Today let’s try to understand how ColorIndex property in Excel VBA works. It is an easy and effective way to quickly complete the development. ColorIndex property is normally used by VBA…
Excel Add-in helps you to extend the features of Excel application. Using Excel Add-in, you can perform custom actions in Excel such as formatting the data, doing complex calculations which are not possible through Excel formulas, Reading or Writing data in other Excel files and so many more actions.
VBA Code to list Files in Folder To work on multiple files through VBA programming, you need to have VBA code that can list files in a folder. In this article we will learn three…
This Excel VBA Code helps to Get User Name. Here is an example environ(username) or Application.username.This macro gets the username from active directory.
VBA Code to Sort Data HERE IS A ONE LINE CODE FOR DEVELOPER’S REFERENCE WHICH CAN BE USED TO SORT DATA ‘Sort data in ascending order on Column F (Created At) Sheet1.Range(“A1:G” & Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Row).Sort Key1:=Sheet1.Range(“F1”),…
In MS Access, the best way to create a multiuser tool is to divide your solution. One part acts as interface and other one acts as database. You can have multiple copies of the interface distributed to users which are connected to central MS Access database saved at common shared drive. To connect the interface to database, you can use link table feature (Access>External Data>Import & Link) available in MS Access. Below is a commonly required VBA code which helps the developers to re-link MS Access linked tables when the database is renamed or moved to other location
GOOD IDEA