Validation is one of the important parts of any programming language. As per few studies, 60% of the code is focused on validating input or output.
In this article, we will explain 2 methods to validate if the given folder path is valid or not.
The below VBA function uses the Dir VBA function to validate Folder Path.
'This function checks if given folder path is valid or not
Public Function CheckFolderExist(strFolderPath As String) As Boolean
'If Dir retunrs blank then it is invalid folder path
If Dir(strFolderPath, vbDirectory) = "" Then
CheckFolderExist = False
MsgBox "Invalid Folder Path!", vbCritical
'Else it is a valid folder path
Else
CheckFolderExist = True
MsgBox "Valid Folder Path!", vbInformation
End If
End Function
Explanation: If the function returns True then it is a valid folder path. If function returns False then it is invalid folder path.
Below VBA function uses File System Object to validate Folder path
'This function checks if given folder path is valid or not
'Microsoft Scripting Runtime reference is required to run this code
Public Function CheckFolderExist(strFolderPath As String) As Boolean
Dim objFileSystem As FileSystemObject
Set objFileSystem = New FileSystemObject
'If FolderExists function returns True then it is valid folder path
If objFileSystem.FolderExists(strFolderPath) = True Then
CheckFolderExist = True
MsgBox "Valid Folder Path!", vbInformation
'Else it is invalid folder path
Else
CheckFolderExist = False
MsgBox "Invalid Folder Path!", vbCritical
End If
End Function
Explanation: If the function returns True then it is a valid folder path. If function returns False then it is invalid folder path.
Have you ever felt that Microsoft should have added a formula in Excel which can count the cells based on specific color? I have seen many code requests to share a VBA code that can count the cells by its color. To help our subscribers and developers, we are sharing 2 codes that be used to count the cells with specific color and returns the count of the matching color cells.
How to Find Duplicate Files In excel using VBA? Yesterday I was working on my computer and cleaning the drives to make some more space. I was surprised to see so many files saved at…
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 to Read Excel Data Using Connection String Sometimes as a programmer you need to read heavy (more then 5 MB) Excel files. There are two ways you can read data from Excel files: Open…
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…
VBA CODE TO HIDE MENU RIBBON IN MS ACCESS In MS Access, there are multiple ways to protect your code or tool from un-authorized access and edits. Some developers prefers to hide MS Access ribbons…