VBA Check If File Exits then Delete in Excel. If it is available then Delete file from the specified folder. In the following tutorial we have explained step by step instructions.
VBA Check If File Exits then Delete
Let us see an example macro VBA code for checking If File Exits or not. If it exists then Delete. In below example we are specifying folder path and file name which we are looking for. You can change these as per your requirements. After that checking specified folder is exists or not. If it is exists checking, we are looping through each file. While looping we are checking every file and specified file is same. If it is same then deleting the specified file from that folder.
'VBA Checking If File Exits then Delete
Sub VBAF1_Check_If_File_Exits_then_Delete()
'Variable declaration
Dim sFolderPath As String
Dim sFileName As String, oFile As Object
'Define Folder Path
sFolderPath = "C:\VBAF1\Test\"
'Specify File Name which we we are looking for
sFileName = "Sample.xlsx"
'Create FSO Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Check Specified Folder exists or not
If oFSO.FolderExists(sFolderPath) Then
'Loop through each file in a specified folder
For Each oFile In oFSO.GetFolder(sFolderPath).Files
'Check Specified is exists
If oFile.Name = sFileName Then
'If file is available delete
oFile.Delete
End If
Next
End If
End Sub
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 VBA Text Files VBA Tables
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog





