VBA Delete Empty Folder
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
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
- Please have a check before running specified folder is empty or not.
- After running macro the specified folder has deleted from a location.
- You can find sample screenshot above.
Related Articles
You can also learn complete details like syntax, example and etc by clicking on the following buttons.