跳至主要内容

如何根据Excel中的单元格列表重命名文件夹中的所有图片文件?

Author: Sun Last Modified: 2025-07-31

您是否曾经需要根据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

相关文章:

最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!