VBA Rename folder helps to change existing folder name to specified new name of folder. We use ‘Name’ function in VBA to rename a folder. We have to specify old name and new name of folder. In the following tutorial we have explained step by step instructions to run example macro.
Read more details about VBA Name function : VBA Name Function
Let us see an example macro VBA code to rename a folder or directory. We use VBA Name function to change or rename folder name or directory name. In the below example we are specifying the old name and new name of folder or directory. You can change this path according to your requirement. If specified folder path is not available, it throws an error. To avoid error message, we are checking whether specified folder is exists or not using Dir function. If it is not available, it displays message. You can see the sample screenshot at the end of the macro.
'VBA Rename a Folder Sub VBAF1_Rename_Folder() 'Variable declaration Dim sFolder_OldName As String Dim sFolder_NewName As String 'Existing Folder Name sFolder_OldName = "C:\VBAF1\Sample\" 'Define New Name of folder to change sFolder_NewName = "C:\VBAF1\Test\" 'Check Specified folder exists or not If Dir(sFolder_OldName) <> "" Then 'Rename Folder using Name function Name sFolder_OldName As sFolder_NewName MsgBox "Folder has renamed.", vbInformation, "VBAF1" Else MsgBox "Folder is not available to rename.", vbInformation, "VBAF1" End If End Sub
Output: You can find following screenshot of above macro output for your reference.
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
- Now you can notice old folder name has changed to new specified folder name.
- You can see output message on the screen. You can find sample screenshot after the VBA macro code.
You can also learn complete details like syntax, example and etc by clicking on the following buttons.