如何根據 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 助手:基於以下內容徹底革新數據分析:智慧執行 | 產生程式碼| 建立自訂公式 | 分析資料並產生圖表| 呼叫增強函數…… |
| 熱門功能:尋找、醒目提示或標記重複值 | 刪除空白行 | 合併列或儲存格而不遺失資料 | 不使用公式的四捨五入…… | |
| 高級 LOOKUP:多重條件 VLookup | 多重數值 VLookup | 跨多個工作表 VLookup | 模糊查找…… | |
| 高級下拉列表:快速建立下拉式清單 | 相依式下拉式清單 | 多選下拉式清單…… | |
| 欄位管理員:新增指定數量的欄位|移動欄位|切換隱藏欄位的可見狀態|比較範圍與欄位…… | |
| 精選功能:網格聚焦 | 設計視圖 |增強編輯欄 | 工作簿與工作表管理員 | 資源庫(自動文字)| 日期提取 | 合併工作表 | 加密/解密儲存格 | 依清單傳送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體儲存格/斜體/刪除線……) ...... | |
| 頂尖 15 工具組:12 文字工具(添加文本,刪除特定字符,……)| 50+ 圖表 類型(甘特圖,……)| 40+ 實用公式(基於生日計算年齡,……)| 19 插入工具(插入二維碼,從路徑插入圖片,……)| 12 轉換工具(金額轉大寫,匯率轉換,……)| 7 合併和拆分工具(高級合併行,分割儲存格,……)|……以及更多 |
運用 Kutools for Excel 強化您的 Excel 技能,體驗前所未有的高效能!Kutools for Excel 提供超過 300 項進階功能,大幅提升生產力並節省寶貴時間。立即點擊,取得您最需要的功能……
Office Tab 為 Office 帶來分頁式介面,讓您的工作更輕鬆自在!
- 在 Word、Excel、PowerPoint 中啟用分頁式編輯與閱讀功能,以及 Access、Visio 與 Project。
- 在同視窗的新分頁中開啟並建立多份文件,而非另開新視窗。
- 每天為您提升 50% 的工作效率,並省下數百次滑鼠點擊!
所有 Kutools 增益集,一個安裝程式
Kutools for Office 套件整合了 Excel、Word、Outlook 與 PowerPoint 的增益集,以及 Office Tab Pro,非常適合需要跨多個 Office 應用程式協作的團隊使用!
- 全能套件— 包含 Excel、Word、Outlook 與 PowerPoint 增益集,以及 Office Tab Pro
- 一個安裝程式,一個授權— 數分鐘內即可完成設定(支援 MSI)
- 協同運作更出色— 在多個 Office 應用程式間實現流暢的生產力體驗
- 30 天完整功能試用— 無需註冊,無需信用卡
- 超值之選— 比單獨購買各增益集更省費用


