Skip to main content

How to rename all images names in a folder according to a list of cells in Excel?

Have you ever tried to rename images according to a list of cells in sheet? If so, have you any tricks to quickly handle the job without renaming them one by one? In this article, I introduce two VBA codes to quickly handle this job in Excel.

Rename all images names in a folder


Rename all images names in a folder

To rename all images names in a specified folder, you must list the original names in sheet firstly.

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: Get picture names of 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 out to remind you to select a cell to output the name list. See screenshot:
doc rename picture in a folder 1

4. Click OK and to select the specified folder whose picture names you need to list in the current worksheet. See screenshot:
doc rename picture in a folder 2

5. Click OK. The picture names have been listed on the active sheet.

Then you can rename the pictures.

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: Get Rename Pictures

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 out to remind you to select the original picture names you want to replace. See screenshot:
doc rename picture in a folder 3

4. Click OK, and select the new names you want to replace picture names within the second dialog. See screenshot:
doc rename picture in a folder 4

5. Click OK, a dialog pops out to remind you that the picture names have been replaced successfully.
doc rename picture in a folder 5

6. Click OK and the picture names have been replaced by the cells in the sheet.

doc rename picture in a folder 6
doc arrow down
doc rename picture in a folder 7

Relative Articles:

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

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...

Description


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!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, I ran the code but the original names are not listing in excel. Only 'Picture Name' comes as the header but no data.
This comment was minimized by the moderator on the site
Thanks for the tip...it was of great help!!
This comment was minimized by the moderator on the site
It doesn't ask me the location of the files, so the script runs in vain and my files aren't being renamed
This comment was minimized by the moderator on the site
СПАСИБО БОЛЬШОЕ! Объединяю два каталога товаров в одни для выгрузки в админку. Все картинки должны быть переименованы в соответствии с ID товара.
Вы сэкономили мне кучу времени и нервов. Спасибо :) Сайт обязательно в закладки.
This comment was minimized by the moderator on the site
Hi, It renames only the first 10 pics of the folder, could you please help me out with the changes for 100 pics. Thanks & Regards
This comment was minimized by the moderator on the site
Hi, edvin.I G Lazar, I have tested the code, it can rename all pictures you list, if it only rename first 10 of the folder, please check what is the picture type, the suffix, in the first code, it just supports to list the pictures(".jpg" ".png" ".img" ".gif" ".ioc" ".bmp"), if your picture is not in the types the code list you can manually add it to the code, like+ InStr(1, xFileName, ".png")
This comment was minimized by the moderator on the site
Hi, i've tried using this however running the 'PictureNametoExcel' macro only returns the first photo file path name. The other photos in the folder wont be listed. Any help would be greatly appreciated.

Side note: I've tested the 'RenameFile' Macro and that works perfectly

Thanks
Sam
This comment was minimized by the moderator on the site
Hi Sam, Select the cell range. I guess this is as a result of you selecting just one cell
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations