Files & Folders from File Explorer Export to Excel

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
826
Office Version
  1. 365
Platform
  1. Windows
I have an excel sheet named “Eng Q” that I am needing only the files and folders put into from Z:engineering/program. Just a list of what’s in that main folder. Is this something that can be obtained when I open the excel sheet. Just need file name, date and file size to show up in columns A,B and C.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
is this what you are looking for
VBA Code:
Sub ListFilesInFolder1()
Columns("A:E").ClearContents
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.file

    SourceFolderName = "Z:engineering/program"

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("File Name", "Date Last Modified", "File Size")

    i = 2
    For Each FileItem In SourceFolder.files
        Cells(i, 1) = FileItem.Name
        Cells(i, 2) = FileItem.DateLastModified
        Cells(i, 3) = FileItem.Size
     

        i = i + 1
    Next FileItem

    Set FSO = Nothing

End Sub
 
Upvote 0
Thank you. Does this go in the the worksheet code or does it run by activating a macro?
 
Upvote 0
this one will need to be placed in regular module
if you want to go in sheet module use below code
you will need to enable "Microsoft Scripting Runtime" also for code to work
VBA Code:
Private Sub Worksheet_Activate()
Columns("A:E").ClearContents
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.file

    SourceFolderName = "Z:engineering/program"

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("File Name", "Date Last Modified", "File Size")

    i = 2
    For Each FileItem In SourceFolder.Files
        Cells(i, 1) = FileItem.Name
        Cells(i, 2) = FileItem.DateLastModified
        Cells(i, 3) = FileItem.Size
    

        i = i + 1
    Next FileItem

    Set FSO = Nothing
End Sub
 
Upvote 0
this one will need to be placed in regular module
if you want to go in sheet module use below code
you will need to enable "Microsoft Scripting Runtime" also for code to work
VBA Code:
Private Sub Worksheet_Activate()
Columns("A:E").ClearContents
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.file

    SourceFolderName = "Z:engineering/program"

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("File Name", "Date Last Modified", "File Size")

    i = 2
    For Each FileItem In SourceFolder.Files
        Cells(i, 1) = FileItem.Name
        Cells(i, 2) = FileItem.DateLastModified
        Cells(i, 3) = FileItem.Size
   

        i = i + 1
    Next FileItem

    Set FSO = Nothing
End Sub
I tried inserting the first code you sent into a module and running it and I got a Compile error: User-defined type not defined. On the line with FSO As Scripting.FileSystemObject
 
Upvote 0
you will need to enable "Microsoft Scripting Runtime" for code to work ( I forgot to tell you that in the first post)
in vba go to tools-References and select "Microsoft Scripting Runtime"
 
Upvote 0
you will need to enable "Microsoft Scripting Runtime" for code to work ( I forgot to tell you that in the first post)
in vba go to tools-References and select "Microsoft Scripting Runtime"
That worked thanks. Is there something that could be added so they would show up fromdate modified newest to oldest.
 
Upvote 0
Add below to the end of your code
VBA Code:
If ActiveSheet.AutoFilterMode = True Then
Else
ActiveSheet.Range("A1").AutoFilter
End If

    ActiveSheet.AutoFilter.Sort.SortFields.Clear
   ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key:= _
        Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Solution
Add below to the end of your code
VBA Code:
If ActiveSheet.AutoFilterMode = True Then
Else
ActiveSheet.Range("A1").AutoFilter
End If

    ActiveSheet.AutoFilter.Sort.SortFields.Clear
   ActiveSheet.AutoFilter.Sort.SortFields.Add2 Key:= _
        Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Thanks again. Works great
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

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