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

 จะแสดงชื่อไฟล์ทั้งหมดในโฟลเดอร์และสร้างไฮเปอร์ลิงก์ใน Excel ได้อย่างไร?

สำหรับงานประจำวันของคุณคุณอาจต้องนำเข้าชื่อไฟล์หลายชื่อจากโฟลเดอร์ไปยังแผ่นงานและสร้างไฮเปอร์ลิงก์สำหรับแต่ละไฟล์เพื่อให้เปิดไฟล์ได้อย่างรวดเร็วและง่ายดาย บางทีคุณสามารถคัดลอกและวางชื่อไฟล์และสร้างไฮเปอร์ลิงก์ได้ทีละรายการด้วยตนเอง แต่จะเสียเวลามากหากมีไฟล์หลายร้อยไฟล์ในโฟลเดอร์ วันนี้ฉันจะพูดถึงวิธีการที่รวดเร็วและน่าสนใจในการจัดการกับงานนี้

แสดงชื่อไฟล์ทั้งหมดในโฟลเดอร์และสร้างไฮเปอร์ลิงก์ด้วยรหัส VBA

แสดงชื่อไฟล์ทั้งหมดในโฟลเดอร์และสร้างไฮเปอร์ลิงก์ด้วย Kutools for Excel


หากต้องการแสดงรายการและสร้างไฮเปอร์ลิงก์พร้อมไฟล์จากโฟลเดอร์รหัส VBA ต่อไปนี้สามารถช่วยคุณได้

1. เปิดใช้งานแผ่นงานใหม่เพื่อใส่ผลลัพธ์

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

3. คลิก สิ่งที่ใส่เข้าไป > โมดูลและวางรหัสต่อไปนี้ในไฟล์ โมดูล หน้าต่าง.

VBA: แสดงชื่อไฟล์ทั้งหมดในโฟลเดอร์และสร้างไฮเปอร์ลิงก์

Sub Example1()
'Updateby Extendoffice
    Dim xFSO As Object 
    Dim xFolder As Object
    Dim xFile As Object
    Dim xFiDialog As FileDialog
    Dim xPath As String
    Dim I As Integer
    Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
    If xFiDialog.Show = -1 Then
        xPath = xFiDialog.SelectedItems(1)
    End If
    Set xFiDialog = Nothing
    If xPath = "" Then Exit Sub
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFSO.GetFolder(xPath)
    For Each xFile In xFolder.Files
        I = I + 1
        ActiveSheet.Hyperlinks.Add Cells(I, 1), xFile.Path, , , xFile.Name
    Next
End Sub

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

รายการ doc สร้างไฮเปอร์ลิงก์ 1

5. หลังจากระบุโฟลเดอร์แล้วให้คลิก OK และไฟล์ทั้งหมดในโฟลเดอร์ที่ระบุไว้ในแผ่นงานที่ใช้งานอยู่พร้อมไฮเปอร์ลิงก์ดูภาพหน้าจอ:

รายการ doc สร้างไฮเปอร์ลิงก์ 2

หมายเหตุ: หากมีโฟลเดอร์ย่อยในโฟลเดอร์เฉพาะของคุณไฟล์ในโฟลเดอร์ย่อยจะไม่อยู่ในรายการ


หากต้องการแสดงรายการไฟล์ทั้งหมดทั้งในโฟลเดอร์และโฟลเดอร์ย่อยและสร้างไฮเปอร์ลิงก์สำหรับแต่ละไฟล์ Kutools สำหรับ Excel's รายการชื่อไฟล์ คุณอาจช่วยได้ ด้วยคุณสมบัตินี้ไฟล์ทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อยจะแสดงรายการพร้อมกันพร้อมกับคุณลักษณะบางอย่างเช่นชื่อไฟล์ขนาดไฟล์เวลาที่สร้างขึ้นมีโฟลเดอร์และอื่น ๆ

Kutools สำหรับ Excel : ด้วย Add-in ของ Excel ที่มีประโยชน์มากกว่า 300 รายการทดลองใช้ฟรีโดยไม่มีข้อ จำกัด ใน 30 วัน.

ถ้าคุณได้ติดตั้ง Kutools สำหรับ Excelโปรดทำตามขั้นตอนต่อไปนี้:

1. คลิก Kutools พลัส > นำเข้าส่งออก > รายการชื่อไฟล์ดูภาพหน้าจอ:

2. ตัว Vortex Indicator ได้ถูกนำเสนอลงในนิตยสาร รายการชื่อไฟล์ โปรดดำเนินการดังต่อไปนี้:

(1. ) คลิก รายการ doc สร้างไฮเปอร์ลิงก์ 5 ปุ่มเพื่อเลือกโฟลเดอร์ที่คุณต้องการแสดงชื่อไฟล์

(2. ) ตรวจสอบ รวมไฟล์ในไดเรกทอรีย่อย หากคุณต้องการแสดงชื่อไฟล์ภายในโฟลเดอร์ย่อยให้เลือก รวมไฟล์และโฟลเดอร์ที่ซ่อนอยู่ หากคุณต้องการแสดงรายการไฟล์ที่ซ่อนอยู่

(3. ) ระบุประเภทไฟล์ที่คุณต้องการค้นหาและแสดงรายการภายใต้ไฟล์ ประเภทไฟล์ มาตรา;

(4. ) เลือกหน่วยขนาดไฟล์ที่คุณต้องการแสดง

(5. ) ตรวจสอบ สร้างไฮเปอร์ลิงก์ ที่ด้านล่างซ้ายของกล่องโต้ตอบ

ดาวน์โหลด Kutools for Excel ทันที!

3. จากนั้นคลิก Ok และไฟล์ทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อยได้รับการแสดงรายการในแผ่นงานใหม่พร้อมไฮเปอร์ลิงก์ตามที่คุณต้องการดูภาพหน้าจอ:

รายการ doc สร้างไฮเปอร์ลิงก์ 6

คลิกเพื่อทราบรายละเอียดเพิ่มเติมเกี่ยวกับยูทิลิตี้ Filename List ...

ดาวน์โหลดและทดลองใช้ Kutools for Excel ฟรีทันที!


Kutools สำหรับ Excel: ด้วย Add-in ของ Excel ที่มีประโยชน์มากกว่า 300 รายการให้ทดลองใช้ฟรีโดยไม่มีข้อ จำกัด ใน 30 วัน ดาวน์โหลดและทดลองใช้ฟรีทันที!

บทความที่เกี่ยวข้อง:

จะแสดงรายการไฟล์ทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อยลงในแผ่นงานได้อย่างไร?

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

🤖 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 (17)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How can you change cell and column location? as I have data already in column A, I would like to change the macro to be in column N
This comment was minimized by the moderator on the site
Hello, Diego,
To put the result in Column N, please apply the below code:
Sub Example1()
'Updateby Extendoffice
    Dim xFSO As Object
    Dim xFolder As Object
    Dim xFile As Object
    Dim xFiDialog As FileDialog
    Dim xPath As String
    Dim I As Integer
    Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
    If xFiDialog.Show = -1 Then
        xPath = xFiDialog.SelectedItems(1)
    End If
    Set xFiDialog = Nothing
    If xPath = "" Then Exit Sub
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFSO.GetFolder(xPath)
    For Each xFile In xFolder.Files
        I = I + 1
        ActiveSheet.Hyperlinks.Add Cells(I, 14), xFile.Path, , , xFile.Name
    Next
End Sub


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Hallo zusammen,

ich habe eine bescheidene Frage:
Wie kann ich Eine Liste von Dateinamen in klickbare Links umwandeln query.
Es sind hier Beispiele genannt worden. Dies hat mich aber nicht weiter gebracht.
Hat wer eine Beispieldatei? Wie aus dem Bericht #28151.

Danke!
This comment was minimized by the moderator on the site
Hello friend,

After all the files in the specific folder have been listed in the active worksheet with hyperlinks, you can click the cells to open the hyperlinks. All the filenames are clickable. Please see the screenshots.

But I can't send you my example Excel file, because those hyperlinks listed in the file can only be opened from my own computers. Just use the method to create hyperlinks with files from your specific folder. After the filenames with hyperlinks are listed in a new file. Just click the hyperlinks to open them. Please have a try.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
@skyyang Hi,
Could this code be edited to hyperlink path for files instead of folders. For example, instead of hyperlinking the paths of the folders inside Folder1, it would hyperlink the paths of all files in Folder1 (i need the text to display as the path of the file, not the file name), that would be the perfect use case for me.
appreciate any help you could offer :)
This comment was minimized by the moderator on the site
How would I change this code to have it list folder names rather than file names? And how would I change it to check subfolders for files?
This comment was minimized by the moderator on the site
Hi, Ellie,
To list all folder and subfolder names from a directory, the below VBA code can help you, please try, hopt it can help you.

Sub FolderNames()
Application.ScreenUpdating = False
Dim xPath As String
Dim xWs As Worksheet
Dim fso As Object, j As Long, folder1 As Object
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next
xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
Application.Workbooks.Add
Set xWs = Application.ActiveSheet
xWs.Cells(1, 1).Value = xPath
xWs.Cells(2, 1).Resize(1, 5).Value = Array("Path", "Dir", "Name", "Date Created", "Date Last Modified")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.getFolder(xPath)
getSubFolder folder1
xWs.Cells(2, 1).Resize(1, 5).Interior.Color = 65535
xWs.Cells(2, 1).Resize(1, 5).EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
Sub getSubFolder(ByRef prntfld As Object)
Dim SubFolder As Object
Dim subfld As Object
Dim xRow As Long
For Each SubFolder In prntfld.SubFolders
xRow = Range("A1").End(xlDown).Row + 1
Cells(xRow, 1).Resize(1, 5).Value = Array(SubFolder.Path, Left(SubFolder.Path, InStrRev(SubFolder.Path, "\")), SubFolder.Name, SubFolder.DateCreated, SubFolder.DateLastModified)
Next SubFolder
For Each subfld In prntfld.SubFolders
getSubFolder subfld
Next subfld
End Sub
This comment was minimized by the moderator on the site
How would you code this to make hyperlinks for path and directory
This comment was minimized by the moderator on the site
Hi, yuri,
To solve your problem, please try the following code:

Sub FolderNames()
Application.ScreenUpdating = False
Dim xPath As String
Dim xWs As Worksheet
Dim xRg As Range
Dim fso As Object, j As Long, folder1 As Object
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next
xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
Application.Workbooks.Add
Set xWs = Application.ActiveSheet
Set xRg = xWs.Cells(1, 1)
xRg.Value = xPath
xWs.Hyperlinks.Add Anchor:=xRg, Address:=xPath, TextToDisplay:=xPath
xWs.Cells(2, 1).Resize(1, 5).Value = Array("Path", "Dir", "Name", "Date Created", "Date Last Modified")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.GetFolder(xPath)
getSubFolder folder1
xWs.Cells(2, 1).Resize(1, 5).Interior.Color = 65535
xWs.Cells(2, 1).Resize(1, 5).EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
Sub getSubFolder(ByRef prntfld As Object)
Dim SubFolder As Object
Dim subfld As Object
Dim xRow As Long
Dim xStr As String
Dim xRg As Range
For Each SubFolder In prntfld.SubFolders
xRow = Range("A1").End(xlDown).Row + 1
Cells(xRow, 1).Resize(1, 5).Value = Array(SubFolder.Path, Left(SubFolder.Path, InStrRev(SubFolder.Path, "\")), SubFolder.Name, SubFolder.DateCreated, SubFolder.DateLastModified)
Set xRg = Cells(xRow, 1)
xRg.Worksheet.Hyperlinks.Add Anchor:=xRg, Address:=xRg.Value, TextToDisplay:=xRg.Value
Set xRg = Cells(xRow, 2)
xRg.Worksheet.Hyperlinks.Add Anchor:=xRg, Address:=xRg.Value, TextToDisplay:=xRg.Value
Next SubFolder
For Each subfld In prntfld.SubFolders
getSubFolder subfld
Next subfld
End Sub
This comment was minimized by the moderator on the site
How do you display also the files. Not just folders and subfolders?
This comment was minimized by the moderator on the site
Hello, isabel,
To list file folder and file names, the following code may help you:
Option Explicit
Sub ListFoldersAndFilesWithDialog()
    Dim fso As Object
    Dim mainFolder As Object
    Dim ws As Worksheet
    Dim rowIndex As Long
    Dim folderPath As String
    ' Open folder picker dialog to select main folder path
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            folderPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set mainFolder = fso.GetFolder(folderPath) 
    On Error Resume Next
    Set ws = ThisWorkbook.Sheets("FolderList")
    On Error GoTo 0  
    If ws Is Nothing Then
        Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        ws.Name = "FolderList"
    End If
    ws.Cells.Clear
    rowIndex = 1
    ws.Cells(rowIndex, 1).Value = "Folder Name"
    ws.Cells(rowIndex, 2).Value = "File Name"
    ListFilesInFolder mainFolder, ws, rowIndex + 1
End Sub

Sub ListFilesInFolder(ByVal folder As Object, ByVal ws As Worksheet, ByRef rowIndex As Long)
    Dim file As Object
    Dim subFolder As Object
    Dim linkAddress As String  
    For Each file In folder.Files
        ws.Cells(rowIndex, 1).Value = folder.Name
        ws.Cells(rowIndex, 2).Value = file.Name
        linkAddress = file.Path
        ws.Hyperlinks.Add Anchor:=ws.Cells(rowIndex, 2), Address:=linkAddress
        rowIndex = rowIndex + 1
    Next file
    
    For Each subFolder In folder.SubFolders
        ListFilesInSubFolder subFolder, ws, rowIndex
    Next subFolder
End Sub

Sub ListFilesInSubFolder(ByVal subFolder As Object, ByVal ws As Worksheet, ByRef rowIndex As Long)
    Dim file As Object
    Dim linkAddress As String 
    For Each file In subFolder.Files
        ws.Cells(rowIndex, 1).Value = subFolder.Name
        ws.Cells(rowIndex, 2).Value = file.Name
        linkAddress = file.Path
        ws.Hyperlinks.Add Anchor:=ws.Cells(rowIndex, 2), Address:=linkAddress
        rowIndex = rowIndex + 1
    Next file
    Dim subSubFolder As Object
    For Each subSubFolder In subFolder.SubFolders
        ListFilesInSubFolder subSubFolder, ws, rowIndex
    Next subSubFolder
End Sub


After copying and pasting the code, please click Tools > References, and then choose Microsoft Scripting Runtime from the Reference-VBAproject dialog box.
https://cdn.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-list-files.png

And then, run the code.

Also, you can use our Kutools for Excel's Filename List feature to solve this job quickly and easily.


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
@skyyang Hi,
Could this code be edited to hyperlink path for files instead of folders. For example, instead of hyperlinking the paths of the folders inside Folder1, it would hyperlink the paths of all files in Folder1 (i need the text to display as the path of the file, not the file name), that would be the perfect use case for me.
appreciate any help you could offer :)
This comment was minimized by the moderator on the site
@skyyang Hi,Could this code be edited to hyperlink path for files instead of folders. For example, instead of hyperlinking the paths of the folders inside Folder1, it would hyperlink the paths of all files in Folder1 (i need the text to display as the path of the file, not the file name), that would be the perfect use case for me. appreciate any help you could offer :)
This comment was minimized by the moderator on the site
"VBA: List all filenames in a folder and create hyperlinks" is great! It works perfectly! Thank you for posting! One question... Now that you told us how to add links from folder, is it possible to use those links to get information from say one or two different sheets in each of the files listed? They would be the same cells for each of the files we now have a list for. This would help create a comprehensive contents with active links to otherwise unintelligible file names.
This comment was minimized by the moderator on the site
Great thanks although can not view the files in the selected directory as barry said, it creates hyperlinks for all files in that directory anyway.
This comment was minimized by the moderator on the site
Awesome ..thanks a lot. It really saved my time.
This comment was minimized by the moderator on the site
Running the VBA code listed above, shows the selected directory is empty, when it is full of Excel files.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations