Skip to main content

如何根據Excel中的單元格列表重命名資料夾中的所有圖片文件?

Author: Sun Last Modified: 2025-05-12

您是否曾經需要根據Excel工作表中的名稱列表來重命名資料夾中的多張圖片?一一手動重命名可能會非常耗時,但借助VBA代碼,您可以快速自動化這個過程。

重命名資料夾中的所有圖片文件


重命名資料夾中的所有圖片文件

要重命名指定資料夾中的所有圖片文件,請按照以下步驟操作:

步驟1:將資料夾中的原始文件名導入到Excel的工作表中

1. 按下「Alt + F11」鍵以啟用「Microsoft Visual Basic for Applications」視窗。

2. 點擊「插入」>「模組」,並將以下代碼粘貼到腳本中。

VBA:從資料夾中獲取圖片名稱

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. 按下「F5」鍵運行代碼,會彈出一個對話框提醒您選擇一個單元格以輸出名稱列表。參見截圖:
A screenshot of the dialog to select a cell for outputting the image name list in Excel

4. 點擊「確定」,並選擇需要列出圖片名稱的指定資料夾於當前工作表中。參見截圖:
A screenshot showing the folder selection dialog for listing image names in Excel

5. 點擊「確定」,圖片名稱已列在當前工作表上。

步驟2:根據新的名稱列表重命名圖片文件

1. 按下「Alt + F11」鍵以啟用「Microsoft Visual Basic for Applications」視窗。

2. 點擊「插入」>「模組」,並將以下代碼粘貼到腳本中。

VBA:重命名資料夾中的圖片文件

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. 按下「F5」鍵運行代碼,會彈出一個對話框提醒您選擇要替換的原始圖片名稱。參見截圖:
A screenshot of the dialog to select original picture names in Excel for renaming

4. 點擊「確定」,並在第二個對話框中選擇要用來替換圖片名稱的新名稱。參見截圖:
A screenshot showing the dialog to select new names for replacing picture names in Excel.

5. 點擊「確定」,會彈出一個對話框提醒您圖片名稱已成功替換。
A screenshot of the success message after renaming pictures in Excel

6. 點擊「確定」,資料夾中的圖片名稱將被替換為工作表中單元格的新名稱。

A screenshot showing the original picture names before renaming in the folder
Arrow down
A screenshot showing the renamed picture names in the folder

相關文章:

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!