Skip to main content

Conas gach comhad i bhfillteán agus i bhfofhillteáin a liostáil i mbileog oibre?

An ndearna tú iarracht riamh gach ainm comhaid ó fhillteán a liostáil i mbileog oibre lena n-áirítear na comhaid atá suite laistigh dá fhofhillteáin? Déanta na fírinne, níl aon bhealach díreach ann dúinn na hainmneacha comhaid a liostáil ó fhillteán agus a fho-fhillteán in Excel, áfach, inniu, tabharfaidh mé isteach roinnt cleasanna gasta chun an post seo a réiteach.

Liostaigh gach ainm comhaid san fhillteán agus san fho-fhillteán le cód VBA

Liostaigh gach ainm comhaid san fhillteán agus san fho-fhillteán go tapa agus go héasca le Kutools for Excel


De ghnáth, ní bhíonn gné ionsuite ag Excel chun déileáil leis an tasc seo, ach, is féidir leat an cód VBA seo a leanas a chur i bhfeidhm chun an fhadhb seo a chur i gcrích.

1. Gníomhaigh bileog oibre nua a liostálfaidh na hainmneacha comhaid.

2. Coinnigh síos an ALT + F11 eochracha in Excel, agus osclaíonn sé an Microsoft Visual Basic d’Fheidhmchláir fhuinneog.

3. Cliceáil Ionsáigh > Modúil, agus greamaigh an cód seo a leanas sa Fuinneog an Mhodúil.

Cód VBA: Liostaigh gach ainm comhaid san fhillteán agus san fho-fhillteán

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. Tar éis duit an cód a ghreamú isteach sa Mhodúl, brúigh F5 eochair chun an cód seo a rith, agus a Macraí bosca dialóige aníos, roghnaigh an Príomhliosta ainm macra, agus ansin cliceáil Rith cnaipe, féach ar an scáileán:

liostaíonn doc comhaid i bhfo-fhillteán fillteán 1

5. Agus sa Brabhsáil fhuinneog, roghnaigh an fillteán a theastaíonn uait gach ainm comhaid lena n-áirítear na fofhillteáin a liostáil, féach an scáileán:

liostaíonn doc comhaid i bhfo-fhillteán fillteán 2

6. Tar éis duit an fillteán a shonrú, ansin cliceáil OK cnaipe, agus tá na hainmneacha comhaid go léir san fhillteán agus a fho-fhillteáin liostaithe sa bhileog oibre reatha ó chill A2, féach scáileáin scáileáin:

liostaíonn doc comhaid i bhfo-fhillteán fillteán 3
1
liostaíonn doc comhaid i bhfo-fhillteán fillteán 4

Leis an gcód thuas, ní féidir leat ach ainmneacha na gcomhad a liostáil, uaireanta, ní mór duit tréithe eile a liostáil, mar shampla méid comhaid, cineál comhaid, am cruthaithe, fillteán agus mar sin de. Kutools le haghaidh Excel tá feidhm úsáideach ann - Liosta Ainm comhaid, leis an ngné seo, is féidir leat gach ainm comhaid nó gach cineál sonrach a liostáil go tapa i bhfillteán agus ina fho-fhillteáin.

Kutools le haghaidh Excel : le níos mó ná 300 breiseán áisiúil Excel, saor in aisce le triail gan aon teorannú i 30 lá.

Tar éis a shuiteáil Kutools le haghaidh Excel, déan na céimeanna seo a leanas le do thoil:

1. Cliceáil Fiontar > Iompórtáil & Easpórtáil > Liosta Ainm comhaid, féach ar an scáileán:

2. Sa an Liosta Ainm comhaid bosca dialóige, déan na hoibríochtaí seo a leanas:

ACliceáil liostaíonn doc comhaid i bhfo-fhillteán fillteán 7cnaipe chun an fillteán a theastaíonn uait na hainmneacha comhaid a liostáil a roghnú;

B: Sonraigh an cineál comhaid is mian leat a liostáil ón Cineál comhaid alt;

C: Roghnaigh aonad amháin de mhéid comhaid is mian leat a thaispeáint as an Aonad méid comhaid alt seo.

nótaí: Chun ainmneacha na gcomhad ón bhfo-fhillteán a liostáil, seiceáil le do thoil Cuir comhaid san áireamh i bhfochomhadlann, is féidir leat an Cuir comhaid agus fillteáin i bhfolach san áireamh de réir mar is gá duit. Má sheiceálann tú Cruthaigh hipearnasc rogha, cruthóidh sé hipearnasc do gach ainm comhaid agus fillteán.

Íoslódáil agus triail saor in aisce Anois!

3. Ansin cliceáil OK cnaipe, taispeánadh na comhaid go léir atá san fhillteán roghnaithe agus a fho-fhillteáin leis na tréithe seo a leanas i mbileog oibre nua. Féach an pictiúr:

liostaíonn doc comhaid i bhfo-fhillteán fillteán 8

Cliceáil chun níos mó sonraí a fháil faoin bhfóntas seo Liosta Ainm Comhad.

Íoslódáil agus triail saor in aisce Kutools le haghaidh Excel Now!


Kutools le haghaidh Excel: le níos mó ná 300 breiseán áisiúil Excel, saor in aisce le triail gan aon teorannú i 30 lá. Íoslódáil agus triail saor in aisce Anois!

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
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