DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to Rename Multiple Files using Macros (VBA) in Excel 365?

This post will guide you to know the simple steps to rename multiple files using Macros (VBA) in Excel Office 365. Macros and VBA are really helpful to achieve any kind of task with a few clicks. Let’s see the simple instructions to finish the task quickly.

Rename Multiple Files using Macros:

If you want to rename multiple files in Excel, do as follows.

  • Let us assume that you have multiple files and format for all file names like excel1.xlsx , excel2.xlsx , excel3.xlsx , excel4.xlsx.

Excel Files
Excel Files

  • We are going to rename all those files to another format.
  • To do this, you need to type all the file names into your current worksheet, then create a new column that contains a new file name list.

Enter file names
Enter file names

  • On the Developer Tab, select the Visual Basic option under the Code section.

Select Visual Basic option
Select the Visual Basic option

  • Then you need to copy and paste the code given below.

VBA code to rename files using Macros

Sub RenameMultipleFiles()

With Application.FileDialog(msoFileDialogFolderPicker)

.AllowMultiSelect = False

If .Show = -1 Then

selectDirectory = .SelectedItems(1)

dFileList = Dir(selectDirectory & Application.PathSeparator & “*”)

Do Until dFileList = “”

curRow = 0

On Error Resume Next

curRow = Application.Match(dFileList, Range(“B1:B4”), 0)

If curRow > 0 Then

Name selectDirectory & Application.PathSeparator & dFileList As _

selectDirectory & Application.PathSeparator & Cells(curRow, “D1:D4”).Value

End If

dFileList = Dir

Loop

End If

End With

End Sub

  • You need to save the code by selecting it. Then close the window.

Save the code
Save the code

  • You have to open the sheet containing the data. On the Developer Tab, choose the Macros option in the Code section.

Choose the Macros option
Choose the Macros option

  • You need to make sure that your macro is selected and click the Run button.

Click the Run button
Click the Run button

  • Now, you need to select one directory that contains files you want to rename in the Browser dialog box
  • Then, you have to click the OK button.

Hit the OK button
Hit the OK button

  • Finally, you can go to that directory and see if the file names are changed or not.

Output
Output

Closure:

In this post, you can clearly learn the simple instructions to rename multiple files using Macros (VBA) in Excel Office 365. Kindly, give your feedback in the below comment section. Thanks for visiting Geek Excel. Keep Learning!

Read Ahead:

Top comments (0)