macro to list file names

kane2979

New Member
Joined
Jan 25, 2008
Messages
29
Hi I have a Macro which will list file names in column A, but it does not include the sub folders. Can someone please help me to edit this macro to include sub folders as well. I also want to display the file path in the column C for ech document displayed in the column A

HTML:
Sub ListFiles()
Dim objFSO As FileSystemObject
Dim objFol As Folder
Dim objFiles As Files
Dim objFile As File
Range("A2:A300").Clear
Set objFSO = New Scripting.FileSystemObject
Set objFol = objFSO.GetFolder("c:\test")
Set objFiles = objFol.Files
Range("A2").Select
For Each objFile In objFiles
    ActiveCell = objFile.Name
    ActiveCell.Offset(1, 0).Select
    DoEvents
Next
Columns("A:A").EntireColumn.AutoFit
Set objFSO = Nothing
Set objFol = Nothing
Set objFiles = Nothing
 End Sub
 
The trick is to get the files in a folder, then get a list of all the folders in that folder. Then you rerun your procedure for each of those folders. It looks like the code Joe4 pointed you to does that.

Here's another useful link

http://www.ozgrid.com/forum/showthread.php?t=44464

Also, you could use application.filesearch, as that has folder recursion built in, but since I understand it's not compatible with XL2007, you're better off not going down that route
 
Last edited:
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top