VBA Rename Folder

VBA Rename Folder

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

VBA Rename a Folder or Directory

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.
VBA Rename Folder

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 VBA Tables and ListObjects

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog
Related Posts

Leave a Comment