Loncat ke daftar isi utama

Bagaimana cara mencantumkan semua file di folder dan subfolder ke dalam lembar kerja?

Pengarang: Xiaoyang Terakhir Dimodifikasi: 2020-04-28

Pernahkah Anda mencoba membuat daftar semua nama file dari folder ke dalam lembar kerja termasuk file yang ada di dalam subfoldernya? Faktanya, tidak ada cara langsung bagi kami untuk mencantumkan nama file dari folder dan subfoldernya di Excel, namun, hari ini, saya akan memperkenalkan beberapa trik cepat untuk menyelesaikan pekerjaan ini.

Cantumkan semua nama file dalam folder dan subfolder dengan kode VBA

Cantumkan semua nama file dalam folder dan subfolder dengan cepat dan mudah dengan Kutools for Excel


Biasanya, Excel tidak memiliki fitur bawaan untuk menangani tugas ini, tetapi, Anda dapat menerapkan kode VBA berikut untuk menyelesaikan masalah ini.

1. Aktifkan lembar kerja baru yang akan mencantumkan nama file.

2. Tahan ALT + F11 kunci di Excel, dan itu membuka file Microsoft Visual Basic untuk Aplikasi jendela.

3. Klik Menyisipkan > Modul, dan tempel kode berikut di Jendela Modul.

Kode VBA: Cantumkan semua nama file dalam folder dan subfolder

Sub MainList()
'Updateby Extendoffice
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End Sub
Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
  Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
  rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
  For Each xSubFolder In xFolder.SubFolders
    ListFilesInFolder xSubFolder.Path, True
  Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
Function GetFileOwner(ByVal xPath As String, ByVal xName As String)
Dim xFolder As Object
Dim xFolderItem As Object
Dim xShell As Object
xName = StrConv(xName, vbUnicode)
xPath = StrConv(xPath, vbUnicode)
Set xShell = CreateObject("Shell.Application")
Set xFolder = xShell.Namespace(StrConv(xPath, vbFromUnicode))
If Not xFolder Is Nothing Then
  Set xFolderItem = xFolder.ParseName(StrConv(xName, vbFromUnicode))
End If
If Not xFolderItem Is Nothing Then
  GetFileOwner = xFolder.GetDetailsOf(xFolderItem, 8)
Else
  GetFileOwner = ""
End If
Set xShell = Nothing
Set xFolder = Nothing
Set xFolderItem = Nothing
End Function

4. Setelah menempelkan kode ke dalam Modul, tekan F5 kunci untuk menjalankan kode ini, dan a Macro kotak dialog muncul, pilih Daftar Utama nama makro, lalu klik Run tombol, lihat tangkapan layar:

doc daftar file dalam folder subfolder 1

5. Dan di Browse jendela, pilih folder yang Anda ingin daftar semua nama file termasuk subfolder, lihat tangkapan layar:

doc daftar file dalam folder subfolder 2

6. Setelah menentukan foldernya, lalu klik OK tombol, dan semua nama file di folder dan subfoldernya telah terdaftar ke dalam lembar kerja saat ini dari sel A2, lihat tangkapan layar:

doc daftar file dalam folder subfolder 3
1
doc daftar file dalam folder subfolder 4

Dengan kode di atas, Anda hanya dapat mencantumkan nama file, terkadang Anda perlu mencantumkan atribut lain, seperti ukuran file, jenis file, waktu pembuatan, folder yang berisi, dan sebagainya. Kutools untuk Excel berisi fungsi yang berguna - Daftar Nama File, dengan fitur ini, Anda dapat dengan cepat mencantumkan semua atau jenis nama file tertentu di dalam folder dan subfoldernya.

Kutools untuk Excel : dengan lebih dari 300 add-in Excel yang praktis, gratis untuk dicoba tanpa batasan dalam 30 hari.

Setelah menginstal Kutools untuk Excel, lakukan dengan langkah-langkah berikut:

1. Klik Enterprise > Ekspor Impor > Daftar Nama File, lihat tangkapan layar:

2. di Daftar Nama File kotak dialog, lakukan operasi berikut:

A: Klik doc daftar file dalam folder subfolder 7tombol untuk memilih folder yang Anda ingin daftar nama filenya;

B: Tentukan jenis file yang ingin Anda daftarkan dari Jenis file bagian;

C: Pilih satu unit ukuran file yang ingin Anda tampilkan dari Unit ukuran file bagian.

Note: Untuk mencantumkan nama file dari subfolder, silakan periksa Sertakan file dalam subdirektori, Anda juga dapat memeriksa Sertakan file dan folder tersembunyi sesuai kebutuhan. Jika Anda memeriksa Buat hyperlink opsi, itu akan membuat hyperlink untuk setiap nama file dan folder.

Unduh dan uji coba gratis Sekarang!

3. Lalu klik OK tombol, semua file yang ada di folder yang dipilih dan subfoldernya telah ditampilkan dengan atribut berikut di lembar kerja baru. Lihat tangkapan layar:

doc daftar file dalam folder subfolder 8

Klik untuk mengetahui detail lebih lanjut tentang utilitas Daftar Nama File ini.

Unduh dan uji coba gratis Kutools untuk Excel Sekarang!


Kutools untuk Excel: dengan lebih dari 300 add-in Excel yang praktis, gratis untuk dicoba tanpa batasan dalam 30 hari. Unduh dan uji coba gratis Sekarang!

Alat Produktivitas Kantor Terbaik

馃 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
Comments (22)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Is it possible to combine this with your other VBA to include hyperlinks?

e.g. hyperlinks to all files in a folder including subfolders?
This comment was minimized by the moderator on the site
Hello, Johnny,
To list all filenames in a folder and subfolders, and create hyperlinks to these files in an Excel worksheet, please apply the following code:
Sub ListFilesAndCreateHyperlinks()
    Dim FolderPath As String
    Dim OutputRow As Integer
    Dim FileSystem As Object
    Dim TargetFolder As Object
    Dim File As Object
    
    ' Set the path to the folder from which to list files
    FolderPath = "C:\Your\Folder\Path"  ' Change this to the desired folder path
    
    ' Create an instance of the FileSystemObject
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    Set TargetFolder = FileSystem.GetFolder(FolderPath)
    
    ' Initialize the row for output in Excel
    OutputRow = 1
    
    ' Call the recursive procedure to list files and create hyperlinks
    ListFiles TargetFolder, OutputRow
    
    ' Clean up
    Set File = Nothing
    Set TargetFolder = Nothing
    Set FileSystem = Nothing
End Sub

Sub ListFiles(ByVal Folder As Object, ByRef Row As Integer)
    Dim SubFolder As Object
    Dim File As Object
    
    ' List all files in the folder
    For Each File In Folder.Files
        ' Insert the file name
        With ActiveSheet
            .Cells(Row, 1).Value = File.Name
            .Hyperlinks.Add Anchor:=.Cells(Row, 1), Address:=File.Path, TextToDisplay:=File.Name
        End With
        Row = Row + 1  ' Move to the next row
    Next File
    
    ' Recursively list files in subfolders
    For Each SubFolder In Folder.SubFolders
        ListFiles SubFolder, Row
    Next SubFolder
End Sub


Note: Replace "C:\Your\Folder\Path" in the code with the path of the folder from which you want to list files.

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Nice work, exactly what I was trying to create. But this is 1000% better.
This comment was minimized by the moderator on the site
Sorry... I gave you the wrong code (below), here is the code I modified..
Code:
Sub MainList()
On Error Resume Next
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Dim answer As Variant
answer = False
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.AllowMultiSelect = False
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show = -1 Then
xDir = folder.SelectedItems(1)
Else
Exit Sub
End If
End With

Call ListFilesInFolder(xDir, True)
End Sub

Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1

' Add a space then the Folder Name to the Worksheet
rowIndex = rowIndex + 1
With Application.ActiveSheet.Cells(rowIndex, 1)
.Value = xFolder.Name
.Font.Size = 12
.Font.FontStyle = "Bold Italic"
End With
rowIndex = rowIndex + 1

For Each xFile In xFolder.Files
Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
For Each xSubFolder In xFolder.SubFolders
ListFilesInFolder xSubFolder.Path, True
Next xSubFolder
rowIndex = rowIndex + 1
End If

Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing

Call MainList
End Sub


This comment was minimized by the moderator on the site
I have modified your code to make it recycle and re-run the folder dialog continuously until you press Cancel.Unfornatually it generates some errors.1. If subfolders become involved in a folder selected then the next folder selected becomes out of numerical order.2. If subfolders become involved in a folder selected the Cancel button has to be repeated and repeated depending on how many folders you have added.
Code:
Sub MainList()
'Updateby Extendoffice
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End SubSub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
For Each xSubFolder In xFolder.SubFolders
ListFilesInFolder xSubFolder.Path, True
Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub

any ideas ?
This comment was minimized by the moderator on the site
Does it work on MAC too?
This comment was minimized by the moderator on the site
Hey, so if I have to just extract an extension from the whole list, where should I make changes?
This comment was minimized by the moderator on the site
Really new to VBA. how do i use the above code but have the file path built into it so i don't have to search for it every time?
This comment was minimized by the moderator on the site
My VBA is saying that the variable folder is not defined. Anyone know why this is?
This comment was minimized by the moderator on the site
What was the purpose of the parameter ByVal xIsSubfolders As Boolean?
This comment was minimized by the moderator on the site
Extremamente elegante este c贸digo!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations