ไปยังเนื้อหาหลัก

วิธีเปลี่ยนชื่อรูปภาพทั้งหมดในโฟลเดอร์ตามรายการเซลล์ใน Excel

คุณเคยพยายามเปลี่ยนชื่อภาพตามรายการเซลล์ในแผ่นงานหรือไม่? ถ้าเป็นเช่นนั้นคุณมีเคล็ดลับในการจัดการงานอย่างรวดเร็วโดยไม่ต้องเปลี่ยนชื่อทีละชื่อหรือไม่? ในบทความนี้ฉันแนะนำรหัส VBA สองรหัสเพื่อจัดการงานนี้ใน Excel ได้อย่างรวดเร็ว

เปลี่ยนชื่อรูปภาพทั้งหมดในโฟลเดอร์


เปลี่ยนชื่อรูปภาพทั้งหมดในโฟลเดอร์

ในการเปลี่ยนชื่อภาพทั้งหมดในโฟลเดอร์ที่ระบุคุณต้องแสดงชื่อดั้งเดิมในแผ่นงานก่อน

1 กด Alt + F11 คีย์เพื่อเปิดใช้งานไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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 เพื่อเรียกใช้รหัสและกล่องโต้ตอบจะปรากฏขึ้นเพื่อเตือนให้คุณเลือกเซลล์ที่จะแสดงรายการชื่อ ดูภาพหน้าจอ:
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 1

4 คลิก OK และเพื่อเลือกโฟลเดอร์ที่ระบุซึ่งมีชื่อรูปภาพที่คุณต้องการแสดงในแผ่นงานปัจจุบัน ดูภาพหน้าจอ:
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 2

5 คลิก OK. ชื่อรูปภาพแสดงอยู่ในแผ่นงานที่ใช้งานอยู่

จากนั้นคุณสามารถเปลี่ยนชื่อรูปภาพได้

1 กด Alt + F11 คีย์เพื่อเปิดใช้งานไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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 เพื่อเรียกใช้รหัสและกล่องโต้ตอบจะปรากฏขึ้นเพื่อเตือนให้คุณเลือกชื่อรูปภาพดั้งเดิมที่คุณต้องการแทนที่ ดูภาพหน้าจอ:
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 3

4 คลิก OKแล้วเลือกชื่อใหม่ที่คุณต้องการแทนที่ชื่อรูปภาพภายในกล่องโต้ตอบที่สอง ดูภาพหน้าจอ:
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 4

5 คลิก OKกล่องโต้ตอบจะปรากฏขึ้นเพื่อเตือนให้คุณทราบว่ามีการเปลี่ยนชื่อรูปภาพสำเร็จแล้ว
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 5

6. คลิกตกลงและชื่อรูปภาพถูกแทนที่ด้วยเซลล์ในแผ่นงาน

doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 6
doc ลูกศรลง
doc เปลี่ยนชื่อรูปภาพในโฟลเดอร์ 7

บทความญาติ:

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
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