VBA Check Workbook Exists in Excel. When we are working with Workbooks, better to check Workbook exists or not before start working. If workbook doesn’t available we can check if any path is wrong or file name is wrong. We can correct it and continue work on it. To check whether file exists or not using Dir function in Excel VBA.
Syntax to Determine Workbook Exists or Not using Dir Function
Here is the following syntax to Check Workbook Exists or Not using Dir Function in Excel VBA.
Dir([PathName],[Attributes As vbFileAttribute = vbNormal) As String
Where PathName and Attributes are optional arguments. It returns string type data.
Macro to verify Workbook Exists or Not in Excel VBA
Let us see the following example macro to Check Workbook Exists or Not in Excel VBA.
'VBA Check Workbook Exists in Excel Sub VBA_Check_Workbook_Exists() 'Variable declaration Dim sFilePath As String 'Value assigned to a variable sFilePath = "D:\FolderName\Sample.xlsx" 'Check Workbook exists or not If Dir(sFilePath) = "" Then 'Workbook is not available MsgBox "File doesn't exist.", vbCritical, "File is not available" Else 'Workbook is available MsgBox "File exists.", vbInformation, "File available" End If End Sub
Here is the output screenshot for above macro procedure.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers