How to rename all image files in a folder according to a list of cells in Excel?
Have you ever needed to rename multiple images in a folder based on a list of names in an Excel sheet? Renaming them one by one can be time-consuming, but with the help of VBA codes, you can quickly automate the process.
Rename all image files in a folder
Rename all image files in a folder
To rename all image files in a specified folder, follow these steps:
Step 1: Import the original file names from the folder into a sheet in Excel
1. Press "Alt + F11" keys to enable the "Microsoft Visual Basic for Applications" window.
2. Click "Insert" > "Module" and paste the below code into the script.
VBA: Retrieve Picture Names from a Folder
Sub PictureNametoExcel()
'UpdatebyExtendoffice201709027
Dim I As Long
Dim xRg As Range
Dim xAddress As String
Dim xFileName As String
Dim xFileDlg As FileDialog
Dim xFileDlgItem As Variant
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select a cell to place name list:", "KuTools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xRg = xRg(1)
xRg.Value = "Picture Name"
With xRg.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
xRg.EntireColumn.AutoFit
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
I = 1
If xFileDlg.Show = -1 Then
xFileDlgItem = xFileDlg.SelectedItems.Item(1)
xFileName = Dir(xFileDlgItem & "\")
Do While xFileName <> ""
If InStr(1, xFileName, ".jpg") + InStr(1, xFileName, ".png") + InStr(1, xFileName, ".img") + InStr(1, xFileName, ".gif") + InStr(1, xFileName, ".ioc") + InStr(1, xFileName, ".bmp") > 0 Then
xRg.Offset(I).Value = xFileDlgItem & "\" & xFileName
I = I + 1
End If
xFileName = Dir
Loop
End If
Application.ScreenUpdating = True
End Sub
3. Press "F5" key to run the code, and a dialog pops up to remind you to select a cell to output the name list. See screenshot:
4. Click "OK" and select the specified folder whose picture names you need to list in the current worksheet. See screenshot:
5. Click "OK". The picture names have been listed on the active sheet.
Step 2: Rename the Picture Files Based on a New Name List
1. Press" Alt + F11" keys to enable the "Microsoft Visual Basic for Applications" window.
2. Click "Insert" > "Module" and paste below code to the script.
VBA: Rename Picture Files in a Folder
Sub RenameFile()
'UpdatebyExtendoffice20170927
Dim I As Long
Dim xLastRow As Long
Dim xAddress As String
Dim xRgS, xRgD As Range
Dim xNumLeft, xNumRight As Long
Dim xOldName, xNewName As String
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRgS = Application.InputBox("Select Original Names(Single Column):", "KuTools For Excel", xAddress, , , , , 8)
If xRgS Is Nothing Then Exit Sub
Set xRgD = Application.InputBox("Select New Names(Single Column):", "KuTools For Excel", , , , , , 8)
If xRgD Is Nothing Then Exit Sub
Application.ScreenUpdating = False
xLastRow = xRgS.Rows.Count
Set xRgS = xRgS(1)
Set xRgD = xRgD(1)
For I = 1 To xLastRow
xOldName = xRgS.Offset(I - 1).Value
xNumLeft = InStrRev(xOldName, "\")
xNumRight = InStrRev(xOldName, ".")
xNewName = xRgD.Offset(I - 1).Value
If xNewName <> "" Then
xNewName = Left(xOldName, xNumLeft) & xNewName & Mid(xOldName, xNumRight)
Name xOldName As xNewName
End If
Next
MsgBox "Congratulations! You have successfully renamed all the files", vbInformation, "KuTools For Excel"
Application.ScreenUpdating = True
End Sub
3. Press "F5" key to run the code, and a dialog pops up to remind you to select the original picture names you want to replace. See screenshot:
4. Click "OK", and select the new names you want to replace picture names within the second dialog. See screenshot:
5. Click "OK", a dialog pops up to remind you that the picture names have been replaced successfully.
6. Click "OK", and the picture names in the folder will be replaced with the new names from the cells in the sheet.
![]() |
![]() |
![]() |
Related Articles:
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!