如何根據Excel中的單元格列表重命名資料夾中的所有圖片文件?
您是否曾經需要根據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」鍵運行代碼,會彈出一個對話框提醒您選擇一個單元格以輸出名稱列表。參見截圖:
4. 點擊「確定」,並選擇需要列出圖片名稱的指定資料夾於當前工作表中。參見截圖:
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」鍵運行代碼,會彈出一個對話框提醒您選擇要替換的原始圖片名稱。參見截圖:
4. 點擊「確定」,並在第二個對話框中選擇要用來替換圖片名稱的新名稱。參見截圖:
5. 點擊「確定」,會彈出一個對話框提醒您圖片名稱已成功替換。
6. 點擊「確定」,資料夾中的圖片名稱將被替換為工作表中單元格的新名稱。
![]() |
![]() |
![]() |
相關文章:
最佳 Office 生產力工具
🤖 | 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 中啟用分頁編輯與閱讀。
- 在同一視窗的新分頁中打開與創建多份文件,而非開啟新視窗。
- 提升您的生產力50%,每日可幫您減少數百次鼠標點擊!
所有 Kutools 外掛,一次安裝
Kutools for Office 套裝整合了 Excel、Word、Outlook 和 PowerPoint 的外掛,外加 Office Tab Pro,非常適合需要跨 Office 應用程式協同作業的團隊。





- 全合一套裝 — Excel、Word、Outlook及 PowerPoint 外掛 + Office Tab Pro
- 一鍵安裝,一份授權 — 幾分鐘完成設置(支援 MSI)
- 協同運作更順暢 — Office 應用間無縫提升生產力
- 30 天全功能試用 — 無需註冊、無需信用卡
- 最超值 — 一次購買,節省單獨外掛費用