Skip i'r prif gynnwys

Sut i restru'r holl ffeiliau mewn ffolder ac is-ffolderi i mewn i daflen waith?

A ydych erioed wedi ceisio rhestru'r holl enwau ffeiliau o ffolder i mewn i daflen waith gan gynnwys y ffeiliau sydd wedi'u lleoli yn ei is-ffolderi? Mewn gwirionedd, nid oes unrhyw ffordd uniongyrchol inni restru'r enwau ffeiliau o ffolder a'i is-ffolder yn Excel, fodd bynnag, heddiw, byddaf yn cyflwyno rhai triciau cyflym i ddatrys y swydd hon.

Rhestrwch yr holl enwau ffeiliau mewn ffolder ac is-ffolder gyda chod VBA

Rhestrwch yr holl enwau ffeil mewn ffolder ac is-ffolder yn gyflym ac yn hawdd gyda Kutools ar gyfer Excel


Fel rheol, nid oes gan Excel nodwedd ymgorffori i ddelio â'r dasg hon, ond, gallwch gymhwyso'r cod VBA canlynol i gyflawni'r broblem hon.

1. Ysgogi taflen waith newydd a fydd yn rhestru'r enwau ffeiliau.

2. Daliwch i lawr y ALT + F11 allweddi yn Excel, ac mae'n agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

3. Cliciwch Mewnosod > Modiwlau, a gludwch y cod canlynol yn y Ffenestr Modiwl.

Cod VBA: Rhestrwch yr holl enwau ffeiliau yn y ffolder a'r is-ffolder

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. Ar ôl pasio'r cod i'r Modiwl, pwyswch F5 allwedd i redeg y cod hwn, ac a Macros blwch deialog yn ymddangos, dewiswch y Prif Restr enw macro, ac yna cliciwch Run botwm, gweler y screenshot:

rhestrwch doc ffeiliau yn is-ffolder ffolder 1

5. Ac yn y Pori ffenestr, dewiswch y ffolder rydych chi am restru'r holl enwau ffeiliau gan gynnwys yr is-ffolderi, gweler y screenshot:

rhestrwch doc ffeiliau yn is-ffolder ffolder 2

6. Ar ôl nodi'r ffolder, yna cliciwch OK botwm, ac mae'r holl enwau ffeiliau yn y ffolder a'i is-ffolderi wedi'u rhestru yn y daflen waith gyfredol o gell A2, gweler sgrinluniau:

rhestrwch doc ffeiliau yn is-ffolder ffolder 3
1
rhestrwch doc ffeiliau yn is-ffolder ffolder 4

Gyda'r cod uchod, gallwch chi ddim ond rhestru'r enwau ffeiliau, weithiau, mae angen i chi restru priodoleddau eraill, megis maint ffeil, math o ffeil, amser wedi'i greu, sy'n cynnwys ffolder ac ati. Kutools ar gyfer Excel yn cynnwys swyddogaeth ddefnyddiol - Rhestr Enw Ffeil, gyda'r nodwedd hon, gallwch chi restru'r holl enwau ffeiliau neu fathau penodol yn gyflym mewn ffolder a'i is-ffolderi.

Kutools ar gyfer Excel : gyda mwy na 300 o ychwanegiadau Excel defnyddiol, am ddim i geisio heb unrhyw gyfyngiad mewn 30 diwrnod.

Ar ôl gosod Kutools ar gyfer Excel, gwnewch y camau canlynol:

1. Cliciwch Menter > Mewnforio ac Allforio > Rhestr Enw Ffeil, gweler y screenshot:

2. Yn y Rhestr Enw Ffeil blwch deialog, gwnewch y gweithrediadau canlynol:

A: Cliciwch rhestrwch doc ffeiliau yn is-ffolder ffolder 7botwm i ddewis y ffolder rydych chi am restru'r enwau ffeiliau;

B: Nodwch y math o ffeil rydych chi am ei rhestru o'r Math o ffeiliau adran;

C: Dewiswch un uned maint ffeil rydych chi am ei harddangos o'r Uned maint ffeil adran hon.

Nodyn: I restru'r enwau ffeiliau o'r is-ffolder, gwiriwch Cynhwyswch ffeiliau mewn is-gyfeiriaduron, gallwch hefyd wirio'r Cynhwyswch ffeiliau a ffolderau cudd yn ôl yr angen. Os gwiriwch Creu hypergysylltiadau opsiwn, bydd yn creu hypergysylltiadau ar gyfer pob enw ffeil a ffolder.

Dadlwythwch a threial am ddim Nawr!

3. Yna cliciwch OK botwm, mae'r holl ffeiliau sydd wedi'u cynnwys yn y ffolder a ddewiswyd a'i is-ffolderi wedi'u harddangos gyda'r priodoleddau canlynol mewn taflen waith newydd. Gweler y screenshot:

rhestrwch doc ffeiliau yn is-ffolder ffolder 8

Cliciwch i wybod mwy o fanylion am y cyfleustodau Rhestr Enw Ffeil hwn.

Dadlwythwch a threial am ddim Kutools ar gyfer Excel Nawr !


Kutools ar gyfer Excel: gyda mwy na 300 o ychwanegiadau Excel defnyddiol, am ddim i geisio heb unrhyw gyfyngiad mewn 30 diwrnod. Dadlwythwch a threial am ddim Nawr!

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
Comments (20)
No ratings yet. Be the first to rate!
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!
This comment was minimized by the moderator on the site
Sub "GetFileOwner()" in code above is not used.
This comment was minimized by the moderator on the site
Hello, jumpjack,
The above code works well in my Excel, which Excel version do you use?
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