How to RENAME a Workbook using VBA in Excel

To RENAME an Excel file that is stored on your computer, you need to use the “NAME” statement. In this statement, you need to define the old file name and the new name that you want to apply. But there’s one thing that you need to remember that the file must be closed.

Here I have an Excel file that is stored in the “Data” folder on my desktop and there in this folder “SampleFile” that I want to rename to the “myFile” and code for this would be like the following.

Name "C:\Users\Dell\Desktop\myFolder\SampleFile.xlsx" As _
"C:\Users\Dell\Desktop\myFolder\myNewFile.xlsx"

Steps to use VBA to Rename Excel File

Now, let’s understand this line of code in detail.

rename-a-workbook-using-vba
  1. The name statement with which you need to start the code.
  2. Address of the file with the old name and file extension.
  3. “As” to refer to the new name.
  4. Address of the file with the new name and file extension.

Helpful Links: Run a MacroMacro RecorderVisual Basic EditorPersonal Macro Workbook

To make the name statement a little clearer you can use variables, just like the following code.

you-can-use-a-variables
Sub vba_rename_workbook()
Dim oldName As String
Dim newName As String
oldName = "C:\Users\Dell\Desktop\myFolder\SampleFile.xlsx"
newName = "C:\Users\Dell\Desktop\myFolder\myNewFile.xlsx"
Name oldName As newName
End Sub