VBA RmDir function in Excel is categorized as File and Directory function. This built-in VBA RmDir function removes an empty existing directory or folder in Excel VBA. If the folder or the directory doesn’t exists, returns an error.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA RmDir Function in any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the RmDir function, where we can use this RmDir Function and real-time examples in Excel VBA.
- Overview
- Syntax of VBA RmDir Function
- Parameters or Arguments
- Where we can apply or use VBA RmDir Function?
- Example 1: Delete a Folder(Empty Folder)
- Example 2: Delete a Folder(Nonempty Folder)
- Example 3: Delete a Directory
- Example 4: Delete a Folder
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA RmDir Function
The syntax of the RmDir Function in VBA is
RmDir(Path)
Note: The RmDir Function doesn’t return any value. It deletes an existing folder or directory.
Parameters or Arguments:
The RmDir function/statement has one argument in Excel VBA.
where
Path: It is a mandatory string parameter. The path argument represents the folder or directory to delete.
Where we can apply or use VBA RmDir Function?
We can use this RmDir Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Delete a Folder(Empty Folder)
Here is a simple example of the VBA RmDir function. This below example deletes an empty folder.
'Delete a Folder(Empty Folder) Sub VBA_RmDir_Function_Ex1() 'Variable declaration Dim sPath As String 'Note: Specified folder is an empty folder. sPath = "C:\Test" RmDir sPath MsgBox "Deleted Folder : " & vbCrLf & sPath, vbInformation, "VBA RmDir Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Delete a Folder(Nonempty Folder)
Here is a simple example of the VBA RmDir function. This below example delete files from the specified folder. And then deletes an empty folder. Here used kill statement to delete files. And used wild card character(*) to delete all available files.
'Delete a Folder(UnEmpty Folder) Sub VBA_RmDir_Function_Ex2() 'Variable declaration Dim sPath As String sPath = "C:\Test" If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 'Delete all files in the folder Kill sPath & "*.*" RmDir sPath MsgBox "Deleted Folder : " & vbCrLf & sPath, vbInformation, "VBA RmDir Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Delete a Directory
Here is a simple example of the VBA RmDir function. This below example deletes an empty directory.
'Delete a Directory Sub VBA_RmDir_Function_Ex3() 'Variable declaration Dim sPath As String 'Note: Specified path is an empty directory. sPath = "C:\Test1\" RmDir sPath MsgBox "Deleted Directory : " & vbCrLf & sPath, vbInformation, "VBA RmDir Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Delete a Folder or Directory
Here is a simple example of the VBA RmDir function. This below example can’t delete specified folder. Because, specified folder is not available.
'Delete a Folder or Directory Sub VBA_RmDir_Function_Ex4() 'Variable declaration Dim sPath As String 'Note: Specified folder is not available. sPath = "C:\Test\" RmDir sPath MsgBox "Deleted Folder : " & vbCrLf & sPath, vbInformation, "VBA RmDir Function" End Sub
Output: Here is the screen shot of the fourth example output.
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 in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers