VBA in Excel does not restrict us to just a single application. VBA gives us access to the windows environment too. With this we can perform a lot of common file-based actions. One of the most common is to rename a file. In this post, we will look at 5 examples of renaming files with VBA.
Table of Contents
Rename a File VBA
To rename a file with VBA we use the Name command. Name appears in blue because it is a reserved word within VBA.
Example 1: Renaming a file
This example renames a file from Example File.xlsx to Example File Renamed.xlsx.
Sub VBARenameFile()
Name "C:\Users\marks\Documents\Example File.xlsx" As _
"C:\Users\marks\Documents\Example File Renamed.xlsx"
End Sub
Example 2: Rename a file based on cell values
In this example, we rename a file based on cell values. The screenshot below shows the current file name in Cell C2 and the new file name in Cell C4.
We can run the following macro to rename a file using these cell values.
Sub VBARenameFileSheetNames()
Name ActiveSheet.Range("C2") As _
ActiveSheet.Range("C4")
End Sub
Example 3: Move a file with the Name command
Did you notice the Name command requires the file path and file name? Therefore, the Name command doesn’t just rename files but can also move files. For example, the code below moves the file from C:\Users\marks\Documents\ to C:\Users\marks\, but the file name remains the same.
Sub VBAMoveFile()
Name "C:\Users\marks\Documents\Example File.xlsx" As _
"C:\Users\marks\Example File.xlsx"
End Sub
Example 4: Avoiding errors when renaming files
Trying to move files that don’t exist, or are locked for editing can trigger errors. The errors are detailed in the section below.
If there is an error, we really want to avoid going through the Visual Basic error debugging process. Instead, a better option is to display a message box with an OK button.
Sub VBAAdvancedRenameFile()
'Create variables to hold file names
Dim filePath As String
Dim newFilePath As String
filePath = "C:\Users\marks\Documents\Example File.xlsx"
newFilePath = "C:\Users\marks\Documents\Example File Renamed.xlsx"
'Ignore errors
On Error Resume Next
'Rename file
Name filePath As newFilePath
'Display message if error occured
If Err.Number <> 0 Then
MsgBox Prompt:="Unable to rename file", Buttons:=vbOK, _
Title:="Rename file error"
End If
'Turn error checking back on
On Error GoTo 0
End Sub
Example 5: Reusable function
Finally, let’s create a reusable function for moving and renaming files.
The VBA function below accepts two string arguments; the existing file path and the new file path.
Function fxRenameFile(filePath As String, newFilePath As String)
'Ignore errors
On Error Resume Next
'Rename file
Name filePath As newFilePath
'Display message if error occured
If Err.Number <> 0 Then
fxRenameFile = False
Else
fxRenameFile = True
End If
'Turn error checking back on
On Error GoTo 0
End Function
We can use this function in two ways.
- Calling the function from another macro
- Calling the function from a worksheet
Let’s look at both of these in turn.
Calling the function from a macro
The macro below calls the function and displays a message box with the following values:
- True = File renamed
- False = Error occurred.
Sub CallFunction()
'Create variables to hold file names
Dim filePath As String
Dim newFilePath As String
filePath = "C:\Users\marks\Documents\Example File.xlsx"
newFilePath = "C:\Users\marks\Documents\Example File Renamed.xlsx"
'True = File Renamed
'False = Error Occured
MsgBox fxRenameFile(filePath, newFilePath)
End Sub
Calling the function from a worksheet
Alternatively, we can call the function just like a normal worksheet function.
Look at the screenshot above, our custom function is used in Cell C6:
=fxRenameFile(C2,C4)
TRUE indicates that the file named in Cell C2 has been successfully renamed to the file named in Cell C4. If we run the function a second time, it will show FALSE, as the file has already been renamed.
Be aware the function executes each time cells C2 or C4 change, so be careful with the order in which you update the cells.
TOP TIP: If we use the fxRenameFile function inside an IF function, it will only executes when the condition is met. In the example below, the fxRenameFile function only executes if cell A6 equals Y.
=IF(A6="Y",fxRenameFile(C2,C4),"Do not rename")
Using this method, we can control when and how the function executes. We just need to change cell A6 to another value when we don’t the function to execute.
Possible errors
If we try to rename a file or folder path that does not exist, it triggers an error: Run-time error’53’: File not found.
If the new file name is the same as an existing one, it triggers the following error: Run-time error ’58’: File already exists.
If either file name is not a valid format, it triggers the following error: Run-time error ‘5’: Invalid procedure call or argument
Notes on renaming files
We have used Excel workbooks in the examples, but we can use any file type. Also, we are not restricted to files; we can rename folders using the Name command too.
The Name command is core Visual Basic code. Therefore, it exists in other applications supporting VBA, such as Word and PowerPoint.
Related Posts:
- VBA code to copy, move, delete and manage files.
- VBA code library
- VBA Copy File – How to + 5 examples
Discover how you can automate your work with our Excel courses and tools.
Excel Academy
The complete program for saving time by automating Excel.
Excel Automation Secrets
Discover the 7-step framework for automating Excel.
Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.