VBA Delete Empty Folder in Excel. We use VBA RmDir VBA function to delete only blank or empty folder. It throws an error, If any file exists in the specified folder. The following tutorial explains examples and step by step instructions for user reference.
Example on VBA Delete Empty or Blank Folder
The following example shows how to delete empty or blank folder. In the following example we have used RmDirfunction to remove blank folder using VBA.
'VBA Delete Blank or Empty Folder Sub VBAF1_Delete_Empty_Folder() 'Variable declaration Dim sFolderPath As String 'Define root folder path to create folder sFolderPath = "C:\VBAF1\Test\" 'Delete Empty Folder RmDir sFolderPath End Sub
Output: Please find the following screenshot for your reference.
Delete Blank Folder using VBA FileSystemObject object
The following is another example to Delete blank Folder using VBA FileSystemObject(FSO) object in Excel. In the following example we have used DeleteFile method of FSO method to delete all files in a folder using VBA.
'VBA Delete empty specified Folder Sub VBAF1_Delete_Empty_Folder_Using_FSO() 'Variable declaration Dim sFolderPath As String 'Define root folder path to create folder sFolderPath = "C:\VBAF1\Test\" 'Create FSO Object Set oFSO = CreateObject("Scripting.FileSystemObject") 'Check Specified Folder exists or not If oFSO.FolderExists(sFolderPath) Then 'Delete All Files from a folder oFSO.DeleteFile sFolderPath & "\*.*", True 'Remove Blank folder RmDir sFolderPath End If End Sub
Output: Please have a check before running specified folder is empty or not. After running macro the specified folder has deleted from a location.
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