Overview of VBA RmDir Function:
VBA RmDir function is categorized as File and Direcetory 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 of VBA RmDir Function
- 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(UnEmpty Folder)
- Example 3: Delete a Directory
- Example 4: Delete a Folder
- Instructions to use Macro Codes
Syntax of VBA RmDir Function
The syntax of the RmDir Function in VBA is
RmDir(Path)
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(UnEmpty 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 use Macro Codes
Here are the step by step instructions to use the RmDir Function in Excel VBA.
- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Copy above specified macros to the VBA editor.
- Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
List of VBA Functions:
Please click on below link to go back to see all List of VBA Functions.