VBA Delete Files in Folder and Subfolder

VBA Delete All Files in Folder and Subfolders

VBA Delete All Files in Folder and Sub folders in Excel. In the following tutorial we are using recursive method. It is nothing but looping through all folders until finishes all available folders. Let us see an example macro code to delete files in folders and sub folders. You can also find the step by step instructions to run VBA code in the visual basic editor(VBE) window.

Delete All Files in Folder and Subfolders using VBA in Excel

Let us see an example macro to Delete All files from a folder and Subfolders using VBA in Excel. The following is a main procedure. In this procedure we are calling sub procedure. The subprocedure helps us to delete files from folder and subfolders. You can change the defined root folder ‘C:\VBAF1’ as per your requirement.

'Main procedure to delete all files in folder and subfolders
Sub VBAF1_All_Delete_Files_in_Folder_and_Subfolders()
    
     'Variable declaration
    Dim sFolderPath As String
    
    'Define Root Folder Path
    sFolderPath = "C:\VBAF1"
    
    'Çall Function
    Call VBAF1_All_Delete_Files_in_Folder_and_Subfolders(sFolderPath)
     
End Sub

Output: You can find following output screenshot for your reference.
VBA Delete Files in Folder and Subfolders

Subprocedure to Delete All Files in Folder and Subfolders

You can find following Subprocedure to Delete All Files in Folder and Subfolders using VBA in Excel. Here we have followed recursive procedure. We are looping through all subfolders to delete available files from it.

'Subprocedure to Delete All Files in Folder and Subfolders
Sub VBAF1_Function_T0_Delete_All_Files(sFldPath As String)
    
    'Variable declaration
    Dim sFolderPath As String
    Dim sFileName As String, oFile As Object, oFolder As Folder
        
    'Check for slash
    If Right(sFldPath, 1) <> "\" Then sFldPath = sFldPath & "\"
        
    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(sFldPath)
    
    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFldPath) Then
        'Loop through each file in a specified folder
        For Each oFile In oFolder.Files
            If oFile.Name <> ThisWorkbook.Name Then
                'Delete file
                oFile.Delete
            End If
        Next
     End If
     
    'Check Subfolders in specified folder
    For Each oSubFolder In oFolder.SubFolders
        'Recursive metod
        'Loop through all subfolders
        VBAF1_Function_T0_Delete_All_Files (sFldPath & oSubFolder.Name)
    Next
    
End Sub

Instructions to use VBA Macro Code

Here are the instructions to use above macro in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can view output in the specified main folder and subfolders to check all the files are deleted or not.
  • you can find above specified input and output screenshot for your reference and better understand.

Related Articles

You can also learn complete details like syntax, example and etc by clicking on the following buttons.

VBA File and Directory Functions in Excel Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *