You rename a file from Excel VBA with the built-in Name statement: Name oldPath As newPath. Both arguments must include the full folder path and file extension, and the destination must not already exist. That single line is the whole mechanism — everything else below is about building the paths safely, handling errors, and knowing the limits so a macro never quietly destroys or overwrites a file.

What You'll Need

  • Excel on Windows or Mac with macros enabled (the Name statement works on both, but path syntax differs slightly).
  • The exact folder path containing the file, and the current file name including its extension.
  • Write permission on that folder — a read-only network share or a synced cloud folder that is locked will fail.
  • Basic familiarity with the VBA editor: press Alt+F11 (Windows) or Option+F11 (Mac) to open it, then Insert > Module.

Menu labels in the VBA editor and in Windows Explorer can differ slightly by version, device and region, so if a label you see does not match exactly, look for the closest equivalent.

Step-by-Step

  1. Open the VBA editor and add a module. In Excel press Alt+F11 (Windows) or Option+F11 (Mac), then choose Insert > Module. A blank code window appears.
  2. Write the simplest rename first. Paste this and edit the two paths:
    Sub RenameOneFile()
        Dim oldPath As String, newPath As String
        oldPath = "C:\Reports\sales_old.xlsx"
        newPath = "C:\Reports\sales_2024.xlsx"
        Name oldPath As newPath
    End Sub
    The Name statement moves and renames in one operation. If the folder is the same, only the file name changes.
  3. Build paths from cells instead of hard-coding them. Hard-coded strings break the moment a folder moves. A more practical pattern reads the folder from a cell and joins it with the file name:
    Dim folder As String, oldName As String, newName As String
    folder = ThisWorkbook.Sheets("Setup").Range("B1").Value
    oldName = "invoice_001.pdf"
    newName = "invoice_001_paid.pdf"
    Name folder & Application.PathSeparator & oldName _
         As folder & Application.PathSeparator & newName
    Application.PathSeparator returns the correct slash for the operating system, so the same code works on Windows and Mac.
  4. Check that the source exists before renaming. The Name statement throws a runtime error if the source is missing. Guard it with Dir:
    If Dir(oldPath) = "" Then
        MsgBox "File not found: " & oldPath
        Exit Sub
    End If
    This turns a confusing error dialog into a readable message.
  5. Check that the destination does not exist. Name will not overwrite an existing file — it raises a "File already exists" error instead. Decide deliberately what should happen:
    If Dir(newPath) <> "" Then
        MsgBox "A file with that name already exists. Rename cancelled."
        Exit Sub
    End If
    Never work around this by deleting the destination first unless you are certain it is disposable.
  6. Wrap the rename in error handling. Even with the checks above, the file can be locked by another program or the folder can be read-only. Add a handler so the macro fails gracefully:
    On Error GoTo Failed
    Name oldPath As newPath
    On Error GoTo 0
    MsgBox "Renamed successfully."
    Exit Sub
    Failed:
    MsgBox "Rename failed. The file may be open or the folder read-only."
  7. Close the file before renaming it. Windows will not let you rename a workbook, CSV or PDF that is currently open in Excel or another application. If your macro renames the workbook it is running from, that will fail — rename other files, not the active one.
  8. Run the macro. Press F5 with the cursor inside the procedure, or attach it to a button via Developer > Insert > Button (Form Control). Check the folder in File Explorer or Finder to confirm the new name appears and the old one is gone.
  9. Loop over many files if needed. To rename a batch, use Dir to walk the folder and rename each match. Always collect the file names into an array first, because renaming while iterating with Dir can skip files. Test on a copy of the folder before running it on real data.

Common Mistakes

  • Leaving off the extension. Name "C:\a\report" As "C:\a\final" produces a file with no extension, which Windows may then refuse to open with the right program. Always include .xlsx, .pdf and so on.
  • Using a relative path. Name "report.xlsx" As "final.xlsx" resolves against Excel's current directory, which is unpredictable. Always supply the full folder path.
  • Assuming Name overwrites. It does not. If you want overwrite behaviour you must delete the target first — and that is a data-loss step, so warn the user or back up first.
  • Renaming an open file. The macro fails with a permission error. Close the file in every application before running the rename.
  • Forgetting Mac path syntax. Mac paths use : internally in VBA in some contexts and / in others. Using Application.PathSeparator and testing on the target machine avoids most of this.
  • No backup. A rename is reversible only if you know the old name. Log old and new names to a sheet so you can undo a batch run.

Tips

  • Use ThisWorkbook.Path to rename files sitting next to the workbook — it keeps the macro portable.
  • Validate new names: strip characters Windows forbids (\ / : * ? " < > |) before renaming, or the statement fails.
  • Trim spaces from cell-sourced names with Trim(); a trailing space creates a file name that looks identical but is not.
  • For anything more complex — copying, moving across drives, checking timestamps — use the FileSystemObject from the Microsoft Scripting Runtime reference instead of Name.
  • Keep a "dry run" switch: a Boolean that, when true, only writes the intended old and new names to the Immediate window (Ctrl+G) without renaming anything.

Troubleshooting

"File not found" error. The path is wrong, the extension is wrong, or the file is on a drive that is not connected. Print the path with Debug.Print oldPath and compare it character by character with what File Explorer shows.

"Permission denied". The file is open, the folder is read-only, or your account lacks write access. Close the file, check the folder's properties, and if it is a synced cloud folder, pause syncing and retry.

"File already exists". The destination name is taken. Choose a different name or delete the target deliberately after confirming it is disposable.

Macro runs but nothing changes. You may be looking in the wrong folder, or the macro silently exited at the Dir check. Add a message box after the rename to confirm the branch taken.

If that did not work, restart Excel, confirm macros are enabled in the Trust Center, and re-check the path on the machine where the macro actually runs — paths copied from another computer rarely survive intact.

Frequently Asked Questions

Does the VBA Name statement overwrite an existing file?

No. It raises a "File already exists" error. You must delete the target first if you truly want to replace it.

Can I rename a file that is currently open?

No. Close it in Excel and any other application first, or the rename fails with a permission error.

Can I rename the workbook the macro is running from?

Not reliably. The open workbook locks its own file, so rename other files instead and reopen the workbook under its new name manually.

Does this work on Mac?

Yes, the Name statement exists in VBA for Mac, but path separators differ. Build paths with Application.PathSeparator and test on the Mac itself.

How do I rename many files at once?

Loop over the folder with Dir, but collect the names into an array first, then rename each one. Test on a copied folder before touching originals.

Can I undo a rename?

Only by running the reverse rename. Log the old and new names to a sheet so you can reverse a batch run if something goes wrong.