Table of Contents
VBA, or Visual Basic for Applications, can be used to automate tasks in Microsoft Office programs, including deleting folders in a specified directory. This is achieved by writing a code that will access the specific directory and use the “rmdir” command to remove the designated folder. This process can save time and effort, especially when dealing with large numbers of folders. It also allows for flexibility in selecting which folders to delete, as the code can be modified to target specific criteria. Overall, using VBA to delete folders in a specified directory is a efficient and effective way to manage and organize files.
Delete Folders Using VBA (With Examples)
You can use the following methods in VBA to delete folders:
Method 1: Delete All Files in Folder
Sub DeleteFolderContents()
On Error Resume Next
Kill "C:UsersbobbiDesktopMy_Data*.*"
On Error GoTo 0End Sub
This particular macro will delete all of the files in the folder called My_Data.
Method 2: Delete Entire Folder
Sub DeleteFolder()
On Error Resume Next'delete all files in folder
Kill "C:UsersbobbiDesktopMy_Data*.*"
'delete empty folder
RmDir "C:UsersbobbiDesktopMy_Data"
On Error GoTo 0End Sub
This particular macro will delete the entire folder My_Data so that it no longer exists.
The line On Error Resume Next tells VBA that if an error occurs and the folder is not found that no error message should be shown.
We then use On Error GoTo 0 to reset the default error message settings.
If you would like to display an error message if the folder is not found, then simply remove these two lines from the code.
The following examples show how to use each method in practice with the following folder called My_Data that contains three Excel files:

Example 1: Delete All Files in Folder Using VBA
Suppose we would like to use VBA to delete all of the files in the folder called My_Data.
We can create the following macro to do so:
Sub DeleteFolderContents()
On Error Resume Next
Kill "C:UsersbobbiDesktopMy_Data*.*"
On Error GoTo 0End SubOnce we run this macro and open the folder again, we will see that all of the files have been deleted:

Example 2: Delete Entire Folder Using VBA
If you would like to use VBA to delete the entire folder called My_Data so that it no longer exists, you can create the following macro:
Sub DeleteFolder()
On Error Resume Next'delete all files in folder
Kill "C:UsersbobbiDesktopMy_Data*.*"
'delete empty folder
RmDir "C:UsersbobbiDesktopMy_Data"
On Error GoTo 0End SubOnce we run this macro and open the File Explorer, we will see that the folder called My_Data no longer exists:

Cite this article
stats writer (2024). How can I use VBA to delete folders in a specified directory?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-folders-in-a-specified-directory/
stats writer. "How can I use VBA to delete folders in a specified directory?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-folders-in-a-specified-directory/.
stats writer. "How can I use VBA to delete folders in a specified directory?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-folders-in-a-specified-directory/.
stats writer (2024) 'How can I use VBA to delete folders in a specified directory?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-folders-in-a-specified-directory/.
[1] stats writer, "How can I use VBA to delete folders in a specified directory?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use VBA to delete folders in a specified directory?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
