Files & Folders from File Explorer Export to Excel - Code Issue

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
When I run the code code below which copies the files name, date and size to my worksheet I am having a couple issues. I believe the code is suppose to be putting them in date modified from newest to oldest but for some reason it is not showing up like that. Also im getting a run time error 1004 all of the sudden.

VBA Code:
Sub ListFilesInFolder1()
Columns("A:E").ClearContents
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.file

    SourceFolderName = "K:\Engineering\Job Release"

    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


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
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I tried the code and in terms of functionality it works well.
The sorting is working properly - newest files are moved to the top.
I didn't get any RTE error-messages. Where do you get RTE-1004?
 
Upvote 0
I tried the code and in terms of functionality it works well.
The sorting is working properly - newest files are moved to the top.
I didn't get any RTE error-messages. Where do you get RTE-1004?
Thanks for your response. The RTE reads " The sort reference is not valid, Make sure that its within the data you want to sort, and the first sort by box isnt the same or blank". When I select the Debug it highlights the code at the bottom of the script ".Apply"
 
Upvote 0
maybe something's wrong with your autofilter. your rely on Excel to autodetect your data range properly, but something may have gone wrong.
I get no such error. And I cannot find a way to replicate the behaviour you talk about.
Try to inspect the objects and variables in the Locals Window in VBE while in debug mode and see if something's out of order.
For example check the sort range in the immediate window:
VBA Code:
?ActiveSheet.AutoFilter.Sort.rng.address
Or check if some object is Nothing when it should be something.
 
Upvote 0
Solution
maybe something's wrong with your autofilter. your rely on Excel to autodetect your data range properly, but something may have gone wrong.
I get no such error. And I cannot find a way to replicate the behaviour you talk about.
Try to inspect the objects and variables in the Locals Window in VBE while in debug mode and see if something's out of order.
For example check the sort range in the immediate window:
VBA Code:
?ActiveSheet.AutoFilter.Sort.rng.address
Or check if some object is Nothing when it should be something.
ok. Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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